2012-09-23 238 views
0

我遇到了与选择的选择的问题。Linq在实体框架中

选择用户标识的()中的select查询不被IDE所喜欢。你如何在选择中完成选择?

Dim newctx As teckModel.teckEntities 
     Dim pending = From c In newctx.my_aspnet_membership Where c.userId = (From c In newctx.my_aspnet_usersinroles Where c.roleId = 8 Select c.userId) Select c.Email, c.CreationDate, c.LastLoginDate 

回答

1

等于运算假定一个值,但第二个LINQ语句返回一个IEnumerable。您应该在IEnumerable上使用.Contains()或者进一步优化子查询以仅返回一个实体。

var pending = from x in newctx.my_aspnet_membership where 
    (from y in newctx.my_aspnet_usersinroles where y.roleId==8 select y.userId).Contains(x.userId) 
    select new { x.Email, x.CreationDate, x.LastLoginDate }; 
+0

谢谢。我想从另一个表中添加额外的选择,其中来自该表的id等于用户标识,我想从该表中捕获该名称。我如何嵌套,以便从先前的查询中检索4个值,名称和三个值? – dinotom

+0

额外的问题是我应该从userid中获取所有的字段,并且只使用我在gridview中需要的字段? – dinotom

+0

您可以使用'Join'来“嵌套”第二个表格,而不是使用子查询。只要您只枚举一次IEnumerable,这应该保持高效。 – lukiffer

1

您可以做一个ContainAny或者你将不得不使用FirstOrDefault

Where (From c In newctx.my_aspnet_usersinroles Where c.roleId = 8 Select c.userId).Contains(c.userId) 

c.userId == (From c In newctx.my_aspnet_usersinroles Where c.roleId = 8 Select c.userId).FirstOrDefault()