我们在linq中设置了所有(值),当我在set参数中使用long列表时一切正常。
List<long> searchIds = new List<long>();
searchIds.Add(1);
using (ClearWhiteDBEntities cwContext = new ClearWhiteDBEntities())
{
var adsWithRelevantadFields =
from adField in cwContext.tblAdFields
join ads in cwContext.tblAds on adField.adId equals ads.id
group adField by adField.adId into adAdFields
where searchIds.All(i => adAdFields.Select(co => co.listId).Contains(i))
select adAdFields.Key;
MessageBox.Show(adsWithRelevantadFields.Count().ToString());
}
但是当我使用类列表时我会收到错误:
var lstId = new ListIds[]
{
new ListIds { listId = 1 },
};
class ListIds
{
public long listId { get; set; }
}
using (ClearWhiteDBEntities cwContext = new ClearWhiteDBEntities())
{
var adsWithRelevantadFields =
from adField in cwContext.tblAdFields
join ads in cwContext.tblAds on adField.adId equals ads.id
group adField by adField.adId into adAdFields
where lstId.All(i => adAdFields.Select(co => co.listId).Contains(i.listId))
select adAdFields.Key;
MessageBox.Show(adsWithRelevantadFields.Count().ToString());
}
问题出在哪里,行 错误是:
无法创建类型为'ClearWhite.Handlers.ListIds的常量值。在此上下文中仅支持原始类型(例如Int32,String和Guid')。
答案 0 :(得分:1)
错误信息非常简单。实体框架无法从您的查询中生成正确的SQL。
您可以在进行查询之前获得List<long>
:
var ids = lstId.Select(i => i.listId).ToList();
然后在查询中使用它:
where ids.All(i => adAdFields.Select(co => co.listId).Contains(i))
或尝试稍微更改您的查询(但我不确定它会做什么):
where lstId.Select(i => i.listId).All(i => adAdFields.Select(co => co.listId).Contains(i))