![](/img/trans.png)
[英]LINQ to Entities does not recognize the method 'System.Collections.Generic.IEnumerable`1
[英]DefaultIfEmpty() Causing “System.NotSupportedException: LINQ to Entities does not recognize the method 'System.Collections.Generic.IEnumerable'”
场景 :我有一个用户配置文件表和一个同事表。 同事表为用户配置文件所跟随的其他用户提供了一条记录。 在此查询中,我将捕获所有关注当前登录用户的人员(从“同事”表中获取其记录ID,并加入“用户个人资料”表以获取其详细信息)。 然后,我再次加入“同事”表,以查看登录的用户是否正在关注该用户。
问题 :据我了解,进行LEFT JOIN的最佳方法(在查找用户跟踪同事的同事记录时)是在该联接中添加“ DefaultIfEmpty()”。 当我向该联接添加.DefaultIFEmpty()时,出现以下错误消息:
System.NotSupportedException:实体的LINQ无法识别方法'System.Collections.Generic.IEnumerable'
如果我从该联接中删除“ .DefaultIFEmpty()”,它将起作用。 但是,它作为常规JOIN运行,遗漏了用户未跟随同事的记录。
代码 :这是我正在使用的代码:
var results = (from a1 in db.Colleague
join b1 in db.UserProfile on new {ColleagueId = a1.ColleagueId} equals
new {ColleagueId = b1.RecordId}
join d1 in db.UserProfile on new {RecordId = a1.OwnerId} equals
new {RecordId = d1.RecordId}
join c1 in db.Colleague
on new {OwnerId = b1.RecordId, Ignored = false, ColleagueId = a1.OwnerId}
equals new {c1.OwnerId, c1.Ignored, c1.ColleagueId} into c1Join
from c1 in c1Join.DefaultIfEmpty() // This is the .DefaultIfEmpty() breaking the query
where
b1.AccountName == userName &&
a1.Ignored == false
orderby
b1.LastName
select new
{
RecordId = (System.Int64?) d1.RecordId,
d1.AccountName,
d1.PreferredName,
d1.FirstName,
d1.LastName,
d1.PictureUrl,
d1.PublicUrl,
IsFollowing = c1.OwnerId < 1 ? 0 : 1
});
foreach (var result in results) // This is what throws the error
{
// Do stuff
}
有任何想法吗?
.NET框架3.5版中的Entity Framework的SQL提供程序不支持DefaultIfEmpty()。 抱歉,但找不到比本文更好的参考: http : //smehrozalam.wordpress.com/2009/06/10/c-left-outer-joins-with-linq/
您可能要尝试直接使用LINQ-to-SQL而不是ADO.NET实体框架。 我相信它可以在LINQ-to-SQL中使用。 过去,我已经验证了通过LinqPad在3.5中进行左联接的工作。
似乎您需要创建一个默认值以传递给DefaultIfEmpty(),因为DefaultIsEmpty()需要一个参数。
仅EFv4 +支持DefaultIfEmpty
。 EF的第一个版本不支持DefaultInEmpty
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.