2011-10-06 92 views
0

我需要与我认为是一个复杂的实体框架4.1表查询的指导。实体框架多表查询

enter image description here

项目具有相关联的标识的列表。用户然后有一个可以选择的身份列表。所以我只想返回具有用户列表中的身份并被选中的项目。有关如何最好地解决这个问题的任何建议?在此先感谢

回答

2

事情是这样的:

var results = db.UserIdentities 
       .Where(x => x.UserId == someUserId && x.Selected) 
       .Select(x => x.Identity.Item);  
+0

感谢您的快速反应。我认为这可能有用,我正在测试它。 var results = db.UserIdentities .Where(x => x.UserId == someUserId && x.Selected) .SelectMany(x => x.Identity.Item); – NullReference

+0

@NullReference:如果我正确理解你的设置,你不应该需要'SelectMany()' - UserIdentity和Identity之间不存在1:1关系,Identity和Item之间也是1:1关系? – BrokenGlass

+0

UserIdentity和Identity以及Identity和Item之间存在*:1关系。该模型仍然不是100%完美,我仍然在学习EF。再次感谢您的帮助,至少让我走上了正轨。 – NullReference