LINQ到实体EF4 [英] Linq to Entities EF4
本文介绍了LINQ到实体EF4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有
(属于该组)用户名
,说明
和收集组域模型
I have a Groups domain model with name
,desc
and collection of users
(belonging to the group)
我想获得一个特定的用户所属的所有组。这是我的LinQ语句:
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 query
Cannot compare elements of type 'System.Collections.Generic.ICollection`1'. Only primitive types (such as Int32, String, and Guid) and entity types are supported.
任何帮助是appreciated.Thanks!
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;
这篇关于LINQ到实体EF4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文