使用linq

时间:2019-06-12 12:25:35

标签: c# linq

我正在填充Viewmodel,并尝试获取该记录的CurrencyIds和CurrencyNames,并将其存储在viewmodel的属性中。我正在获取CurrenctIds,但如何获取CurrencyNames。

当前它仅返回一个值。如何查询OtherCurrencyIds并检索值

var currencyLookUp = currencies.Select(x => new LookUpViewModel()
{
    Id = x.CURRENCY_ID, 
    Name = x.CURRENCY_NAME
});

var legalFundClassDetailsViewModel = legalfundClasses.Select(fc => new LegalFundClassDetailsViewModel
{
    OtherCurrencyIds = otherCurrencies
        .Where(x => x.FUND_CLASS_ID == fc.ID)
        .Select(x => x.CURRENCY_ID)
        .ToArray(),
    OtherCurrencyNames = currencyLookUp
        .Where(x => x.Id == fc.CURRENCY_ID)
        .Select(x => x.Name)
        .ToArray()
});

我的全部代码

var legalFundClassDetailsViewModel = legalfundClasses.Select(fc => new LegalFundClassDetailsViewModel
            {

                Description = fc.DESCRIPTION,
                Id = fc.ID,
                FundId = fc.FUND_ID,
                FundClassType = fc.CLASS_TYPE,
                AuditSummary = getAuditSummary(managerStratedyId, fc.ID),
                FeesReviewSummary = getFeesReviewSummary(fc),
                TermsReviewSummary = getTermsReviewSummary(fc),
                CurrencyId = fc.CURRENCY_ID,
                PrimaryCurrencyName = currencyLookUp.Where(x => x.Id == fc.CURRENCY_ID).Select(x => x.Name).FirstOrDefault(),
                OtherCurrencyIds = otherCurrencies.Where(x => x.FUND_CLASS_ID == fc.ID).Select(x => x.CURRENCY_ID).ToArray(),
                //OtherCurrencyNames = currencyLookUp.Where(x => x.Id == fc.CURRENCY_ID).Select(x => x.Name).SingleOrDefault(),
                ManagerStrategyId = fc.MANAGERSTRATEGY_ID,
                ManagerStrategyName = managerStrategiesLookUp.Where(x => x.Id == fc.MANAGERSTRATEGY_ID).Select(x => x.Name).FirstOrDefault(),
                SubVotingId = fc.SUB_VOTING_ID,
                SubVotingName = liqVotingLookUp.Where(x => x.Id == fc.SUB_VOTING_ID).Select(x => x.Name).FirstOrDefault(),
                SubHotIssueId = fc.SUB_HOT_ISSUE_ID,
                SubHotIssueName = liqHotIssueLookUp.Where(x => x.Id == fc.SUB_HOT_ISSUE_ID).Select(x => x.Name).FirstOrDefault(),
                RedsFrqncyId = fc.REDS_FRQNCY_ID,
                RedsFrqncyName = liqRedsFrequencyLookUp.Where(x => x.Id == fc.REDS_FRQNCY_ID).Select(x => x.Name).FirstOrDefault(),
                RedsNoticeDays = fc.REDS_NOTICE_DAYS,
                NoticeTypeOfDaysId = fc.NOTICE_TYPE_OF_DAYS_ID,
                NoticeTypeOfDaysName = liqTypeOfDaysLookUp.Where(x => x.Id == fc.NOTICE_TYPE_OF_DAYS_ID).Select(x => x.Name).FirstOrDefault(),
                LockupTypeId = fc.LOCKUP_TYPE_ID,
                LockupTypeName = liqLockupTypeLookUp.Where(x => x.Id == fc.LOCKUP_TYPE_ID).Select(x => x.Name).FirstOrDefault(),
                HardDurationMonthsId = fc.HARD_DURATION_MONTHS_ID,
                HardDurationMonthsName = liqDurationMonthsLookUp.Where(x => x.Id == fc.HARD_DURATION_MONTHS_ID).Select(x => x.Name).FirstOrDefault(),
                SoftDurationMonthsId = fc.SOFT_DURATION_MONTHS_ID,
                SoftDurationMonthsName = liqDurationMonthsLookUp.Where(x => x.Id == fc.SOFT_DURATION_MONTHS_ID).Select(x => x.Name).FirstOrDefault(),
                LockupFees0To12Pct = fc.LOCK_UP_FEES_0_12_IN_PCT,
                LockupFees12To24Pct = fc.LOCK_UP_FEES_12_24_IN_PCT,
                LockupFees24To36Pct = fc.LOCK_UP_FEES_24_36_IN_PCT,
                WebfolioRedsFee = fc.WEBFOLIO_REDS_FEE,
                LockupComments = fc.LOCK_UP_COMMENTS,
                ApplyGateDecliningBalance = fc.APPLY_GATE_DECLINING_BALANCE,
                GateInvestorPct = fc.GATE_INVESTOR_PCT,
                GateSourceId = fc.GATE_SOURCE_ID,
                GateSourceName = liqGateSourceLookUp.Where(x => x.Id == fc.GATE_SOURCE_ID).Select(x => x.Name).FirstOrDefault(),
                GateFundClassPct = fc.GATE_FUND_CLASS_PCT,
                IntialProceeds = fc.INITIAL_PROCEEDS,
                PaymentInDays = fc.PAYMENT_IN_DAYS,
                PaymentTypeOfDaysId = fc.PAYMENT_TYPE_OF_DAYS_ID,
                PaymentTypeOfDaysName = liqTypeOfDaysLookUp.Where(x => x.Id == fc.PAYMENT_TYPE_OF_DAYS_ID).Select(x => x.Name).FirstOrDefault(),
                HoldbackPercentage = fc.HOLDBACK_PERCENTAGE,
                HoldbackPayment = fc.HOLDBACK_PAYMENT,
                HoldbackTypeOfDaysId = fc.HOLDBACK_TYPE_OF_DAYS_ID,
                HoldbackTypeOfDaysName = liqTypeOfDaysLookUp.Where(x => x.Id == fc.HOLDBACK_TYPE_OF_DAYS_ID).Select(x => x.Name).FirstOrDefault(),
                ManagementFeeRate = fc.MANAGEMENT_FEE_RATE,
                IncentiveFeeRate = fc.INCEN_FEE_RATE,
                RealizationFrequencyId = fc.INCEN_REALZN_FQCY_ID,
                RealizationFrequencyName = liqFrequencysLookUp.Where(x => x.Id == fc.INCEN_REALZN_FQCY_ID).Select(x => x.Name).FirstOrDefault(),
                HighWaterMarkId = fc.HIGH_WATER_MARK_ID,
                HighWaterMarkName = liqHighWaterMarksLookUp.Where(x => x.Id == fc.HIGH_WATER_MARK_ID).Select(x => x.Name).FirstOrDefault(),
                HurdleRate = fc.HURDLE_RATE,
                HurdleRateBasisId = fc.HDL_RATE_BASIS_ID,
                HurdleRateBasisName = liqHurdleRateBasisLookUp.Where(x => x.Id == fc.HDL_RATE_BASIS_ID).Select(x => x.Name).FirstOrDefault(),
                HurdleRatePct = fc.HURDLE_RATE_PCT,
                HurdleRateIndexId = fc.HDL_RATE_INDEX_ID,
                HurdleRateIndexName = liqHurdleRateIndexLookUp.Where(x => x.Id == fc.HDL_RATE_INDEX_ID).Select(x => x.Name).FirstOrDefault(),
                PreferredReturnRatePct = fc.PREFERRED_RETURN_RATE_PCT,
                GpCatchUp = fc.GP_CATCH_UP,
                PreferredReturnComments = fc.PREFERRED_RETURN_COMMENTS,
                Clawback = fc.CLAWBACK,
                ClawbackPercentage = fc.CLAWBACK_PERCENTAGE,
                AssetFeeDiscountTypeId = fc.ASSET_FEE_DISCOUNT_TYPE_ID,
                AssetFeeDiscountTypeName = feeDicountTypesLookUp.Where(x => x.Id == fc.ASSET_FEE_DISCOUNT_TYPE_ID).Select(x => x.Name).FirstOrDefault(),
                FeeComments = fc.FEE_COMMENTS,
                FeeReductionsNegotiated = fc.FEE_REDUCTIONS_NEGOTIATED,
                InvestmentStatusId = fc.INVESTMENT_STATUS_ID,
                LegalParentClassId = fc.LEGAL_CLASS_PARENT_ID
            }).ToList();

2 个答案:

答案 0 :(得分:0)

您可以使用匿名类型:

var currencyLookUp = currencies.Select(x => new 
{
    Id = x.CURRENCY_ID, 
    Name = x.CURRENCY_NAME
});

然后

var legalFundClassDetailsViewModel = legalfundClasses.Select(...).Distinct();

答案 1 :(得分:0)

我认为最简单的方法是在select语句内使用货币ID创建一个临时变量。

var currencyLookUp = currencies.Select(x => new LookUpViewModel()
{
    Id = x.CURRENCY_ID, 
    Name = x.CURRENCY_NAME
}).ToList(); // Note I materialized the currencies here.

var legalFundClassDetailsViewModel = legalfundClasses.Select(fc => 
{
    // temp variable
    var ids = otherCurrencies
        .Where(x => x.FUND_CLASS_ID == fc.ID)
        .Select(x => x.CURRENCY_ID)
        .ToArray();

    return new LegalFundClassDetailsViewModel
    {
        OtherCurrencyIds = ids,

        // Query the currency lookup based on the temp variable.
        OtherCurrencyNames = ids.Select(
            id => currencyLookUp.FirstOrDefault(l => l.Id == id)?.Name)
                  .ToArray()
    });
});