gpt4 book ai didi

c# - DefaultIfEmpty() 导致 "System.NotSupportedException: LINQ to Entities does not recognize the method ' System.Collections.Generic.IEnumerable'”

转载 作者:行者123 更新时间:2023-12-02 01:19:00 26 4
gpt4 key购买 nike

场景:我有一张用户个人资料表和一张同事表。 colleagues 表记录了用户配置文件所关注的所有其他用户。在此查询中,我获取了所有关注当前登录用户的人(从 Colleagues 表获取他们的记录 ID 并加入 User Profile 表以获取他们的详细信息)。然后,我再次加入 Colleagues 表以查看登录用户是否正在关注该用户。

问题:据我了解,执行 LEFT JOIN(在查找用户关注同事的同事记录时)的最佳方法是将“DefaultIfEmpty()”添加到加入。当我将 .DefaultIFEmpty() 添加到该连接时,我收到以下错误消息:

System.NotSupportedException: LINQ to Entities does not recognize the method '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 Framework 3.5 版中的 Entity Framework SQL 提供程序不支持 DefaultIfEmpty()。抱歉,找不到比这篇文章更好的引用:http://smehrozalam.wordpress.com/2009/06/10/c-left-outer-joins-with-linq/

您可能想尝试直接使用 LINQ-to-SQL 而不是 ADO.NET Entity Framework 。我相信它适用于 LINQ-to-SQL。我过去已经通过 LinqPad 验证了 left join 在 3.5 中的工作。

关于c# - DefaultIfEmpty() 导致 "System.NotSupportedException: LINQ to Entities does not recognize the method ' System.Collections.Generic.IEnumerable'”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7502220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com