在此上下文中仅支持基本类型或枚举类型 [英] Only primitive types or enumeration types are supported in this context
问题描述
所以我有这个代码:
public int saleCount(List<Shift> shifts) {
var query = (
from x in database.ItemSales
where shifts.Any(y => y.ID == x.shiftID)
select x.SalesCount
);
return query.Sum();
}
不幸的是,它抛出了这个错误:
Unfortunately, it's throwing this error:
Unable to create a constant value of type 'Shift'.
Only primitive types or enumeration types are supported in this context.
所以这里是我定义shift的地方,这只是一个普通的实体框架代码第一个对象: p>
So here is where I define shift, which is just a normal Entity Framework Code First object:
[Table("Shifts")]
public class Shift : MPropertyAsStringSettable {
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public int SiteID { get; set; }
public string ShiftID_In_POS { get; set; }
public DateTime ShiftDate { get; set; }
}
我猜问题是我在Linq查询中使用Shift对象。我正在Shift列表中进行任何操作。
I guess the problem is that I am using Shift object in a Linq query. And I am doing a "Any" operation on a List of "Shift".
一个可能的解决方案是将列表更改为可能的集合或其他东西,在其他地方加载一个linq查询,但签名是什么?
One possible solution is to change the List to maybe a Collection or something, afterall, I am loading it with a linq query somewhere else, but what would the signature be?
推荐答案
尝试此更改不使用在查询中收集 Shift
Try this change that does not use a collection of Shift
s in the query:
public int saleCount(List<Shift> shifts) {
var shiftIds = shifts.Select(s => s.ID).ToList();
var query = (
from x in database.ItemSales
where shiftIds.Contains(x.shiftID)
select x.SalesCount
);
return query.Sum();
}
这个想法是避免通过 Shift
对象到查询提供者,使用ID代替。
The idea is to avoid passing Shift
objects to the query provider, using IDs instead.
这篇关于在此上下文中仅支持基本类型或枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!