2017-01-02 4 views
0

私のテーブルには以下の関係があります。Linqを使用した1-nナビゲーションパスの照会

TravelPlan (Table A)

Destination(table D)Users(table E)が旅行選択肢、すなわち構築するために使用される、Travelplan缶内のエントリは、複数DestinationUser

DestinationTravelPlanMapping(TableC)(マッピングがすなわち1 nに、別のマッピングテーブルに格納されている)を含みます UserTravelPlanMapping(TableD)

が、私はLINQのを使用して、指定されたユーザーのすべてのUniquedestinationsを取得する必要があり、この

に間違ったquery.whatを完了することはできませんよ。

var userId=6; 
return context.TravelPlan 
         .Include(x => x.DestinationTravelPlanMapping) 
         .Include(x => x.DestinationTravelPlanMapping.Select(y => y.Destination)) 
         .Include(x => x.UserTravelPlanMapping.Select(y => y.UserId == userId)) 
// the below select statement is throwing error 
         .Select(x => x.DestinationTravelPlanMapping.Select(y => new Destination 
         { 
          Id = y.Destination.Id, 
          Name = y.Destination.Name 
         })); 
+1

'以下のselect文はエラーをスローしています。 – Reniuz

+0

関数の署名はどのように見えますか? –

+0

1)ここでは '.Include'は必要ありません。 2)関係の図を含めるか、テキストとのエンティティの関係を表す他の方法を見つけることをお勧めします。 –

答えて

0

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

return context.TravelPlans 
    .Where(x => x.UserTravelPlanMapping.Any(y => y.UserID == userID)) 
    .SelectMany(x => x.DestinationTravelPlanMapping.Select(y => y.Destination)) 
    .Distinct(); 

あなたはユニークなDestination秒たい変数userIDに一致UserIDUserTravelPlanMappingを持ってTravelPlansから。

関連する問題