从Linq查询中选择多个列

时间:2017-01-27 13:08:12

标签: c# asp.net linq linq-to-sql

我有两张桌子。客户和客户利益。

每个表中的字段

Customer - ID, Name, Address, Country etc
CUstomerBenefits -  CustomerID, BenefitID (Both IDs have a relation to their relevant tables)

我正在尝试列出客户并获得福利明细

    IEnumerable<CustomerBenefit> GetData = from c in MyContext.CUstomerBenefits
                                                   where c.CustomerId == UserId & c.Active = true
                                                   orderby c.CustomerBenefit.BenType descending
                                                   select new {???? };

对于Select,我试过

select new {c.Benefit.Name }

但得到错误

无法将类型'System.Linq.IQueryable'隐式转换为'System.Collections.Generic.IEnumerable'。存在显式转换(您是否错过了演员?)

如果我将IEnumerable<CustomerBenefit>更改为IEnumerable<dynamic>我没有错误,但是当数据绑定到我的gridview时,我得到错误,即找不到字段。

我如何保留IEnumerable<CustomerBenefit>并选择我需要的列?

3 个答案:

答案 0 :(得分:2)

您写道:

  

CUstomerBenefits - CustomerID,BenefitID(两个ID都与...有关   他们的相关表格)

我认为这意味着有一个表CustomerBenefit包含好处,而CUstomerBenefits表又是客户和利益之间的链接。现在您想获得特定客户拥有的所有好处的列表吗?

这应该可以解决问题:

IEnumerable<CustomerBenefit> GetData =
    from c in MyContext.CUstomerBenefits
    where c.CustomerId == UserId && c.Active == true
    orderby c.CustomerBenefit.BenType descending
    select c.CustomerBenefit;

答案 1 :(得分:1)

使用:select c获取所有列值或:

select new CustomerBenefit {CustomerID = c.CustomerID};

答案 2 :(得分:0)

var HOQuotePremiums = (from holoc in objRes.ResHO.HOLocations
                                               from cst in ((HOLocation)holoc).HOBuildings
                                               select new
                                               {
                                                   RequestId = cst.HOPremium.RequestId,
                                                   BasePremium = Convert.ToInt32(cst.HOPremium.BasePremium),
                                                   PersonalLiabilityPremium = cst.HOPremium.PersonalLiabilityPremium,
                                                   MedicalPaymentPremium = cst.HOPremium.MedicalPaymentPremium,
                                                   FinalPremium = objRes.ResHO.FinalPremium,
                                                   WithoutAOPDeductibleFinalPremium = cst.HOPremium.WithoutAOPDeductibleFinalPremium,
                                                   SubTotalPremium = objRes.ResHO.FinalPremiumWithoutFeeTax,
                                                   LocationID = holoc.LocationID,
                                                   BuildingID = cst.BuildingID,
                                                   IsMinimumPremium = cst.HOPremium.IsMinimumPremium,
                                                   MinimumPremium = cst.HOPremium.MinimumPremium,
                                                   SubTotalPremiumWithoutMinimumPremium = cst.HOPremium.SubTotalPremiumWithoutMinimumPremium,
                                                   FinalRate=cst.HOPremium.FinalRate,
                                               }).ToList();

请试一试。