方案:我有一个User Profiles和一个同事表的表。同事表具有用户配置文件所关注的其他用户的记录。在这个查询中,我抓住所有跟随当前登录用户的人(从同事表中获取他们的记录ID并加入用户配置文件表以获取他们的详细信息)。然后,我再次加入“同事”表,以查看登录用户是否关注该用户。DefaultIfEmpty()导致“System.NotSupportedException:LINQ to Entities无法识别方法'System.Collections.Generic.IEnumerable'”
问题:这是我的理解是,做一个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
}
任何想法?
我会从重新考虑因素开始,让代码更具可读性。您不需要一气呵成,您可以进行中间查询,并且它仍然会作为一个查询发送到SQL Server。您不需要创建所有这些匿名对象,例如,您可以执行a1.ColeagueID == b1.RecordId。完成后,人们可以更容易地计算出其他问题 – ForbesLindesay