2017-01-27 12 views
1

私は2つのテーブルを持っています。顧客と顧客の利益。私は顧客のリストを表示し、選択のために特典の詳細Linqクエリから複数の列を選択

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

を取得しようとしています

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

各テーブルの

フィールドは、アイブ氏は

select new {c.Benefit.Name } 

を試みたが、取得しますエラー

型 'System.Linq.IQueryable'を 'System.Collections.Generic.IEnumerable'に暗黙的に変換することはできません。明示的な変換は、私がIEnumerable<dynamic>IEnumerable<CustomerBenefit>を変更する場合、私はエラーを取得していないが、データは私のGridViewにバインドされたとき、私はフィールドが見つからないエラーが発生します(あなたはキャストが欠けている?)

存在します。

どうすればIEnumerable<CustomerBenefit>を保存して、必要な列を選択できますか?

答えて

2

あなたが書いた:

CUstomerBenefits - 得意先、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

用途:select c列の値の全部または取得するには:

select new CustomerBenefit {CustomerID = c.CustomerID};など

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(); 

これを試してみてください。

関連する問題