Lambda表达式:如何从一个查询中选择两个不相关的表

时间:2012-03-13 07:36:20

标签: c#-4.0 entity-framework-4 lambda linq-to-entities

你能帮我用lambda表达式重写这个查询语法吗?

(From Entity.Apple a in db.Context.Apples
From Entity.Bikini b in db.Context.Bikinis
Where a.Id== 10
Where b.Id== 15
Select new {NaturalColor: a.Color, FavoriteColor: b.Color }).FirstOrDefault();

我尝试查找两个不相关的表的特定记录,一个连接。在我的例子中,下一步可能是:

FindColorDiffrent(Naturalcolor, FavoriteColor){}

1 个答案:

答案 0 :(得分:3)

db.Context.Apples
  .SelectMany(a => db.Context.Bikinis, (a, b) => new {a, b})
  .Where(x => x.a.Id == 10)
  .Where(x => x.b.Id == 15)
  .Select(x => new {NaturalColor: x.a.Color, FavoriteColor: x.b.Color })
  .FirstOrDefault();