dinotom
dinotom

Reputation: 5162

Linq in entity framework

I am having trouble with a select with a select.

The select query in () which is selecting the userid is not liked by the IDE. How do you accomplish a select within a select?

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

Upvotes: 0

Views: 918

Answers (2)

AD.Net
AD.Net

Reputation: 13409

You can either do a Contain or Any or you'll have to use FirstOrDefault

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

or

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

Upvotes: 1

lukiffer
lukiffer

Reputation: 11313

The equals operator assumes a single value, but the second linq statement is returning an IEnumerable. You should use .Contains() on the IEnumerable or further refine the subquery to return only a single entity.

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 };

Upvotes: 1

Related Questions