开发者

Linq to Entities EF4

I have a Groups domain model with name,desc and collection of users(belonging to the group)

I am trying to get all groups that a particular user belongs to. This is my LinQ statement:

var results = from p in AuthorizationService.UnitOfWork.Groups.FindAll()
                          where
                              (p.Users != null && p.Users.Select(u => u.Id).Contains(CurrentUser.Id))
                          select p.Name;

I get the following error when i try to execute the qu开发者_运维技巧ery

Cannot compare elements of type 'System.Collections.Generic.ICollection`1'. Only primitive types (such as Int32, String, and Guid) and entity types are supported.

Any help is appreciated.Thanks!


Remove the null testing for Users object, anyway it's lazy loaded, is your Users virtual? if it is, it is lazy-loaded, it's ok to remove the null testing then

var results = 
from p in AuthorizationService.UnitOfWork.Groups.FindAll() 
where 
     p.Users.Any(u => u.Id == CurrentUser.Id)
select p.Name;


Can't you go the opposite way?

var results = AuthorizationService.UnitOfWork.Users.Include("Groups").First(u => u.ID == CurrentUser.Id).Select(u => u.Groups);

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜