在此上下文中仅支持基元类型或枚举类型

Bil*_*ill 9 c# linq entity-framework

所以我有这个代码:

    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();
    }
Run Code Online (Sandbox Code Playgroud)

不幸的是,它抛出了这个错误:

Unable to create a constant value of type 'Shift'. 
Only primitive types or enumeration types are supported in this context.
Run Code Online (Sandbox Code Playgroud)

所以这里是我定义shift的地方,这只是一个正常的Entity Framework Code First对象:

[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; }
}
Run Code Online (Sandbox Code Playgroud)

我想问题是我在Linq查询中使用Shift对象.我正在对"Shift"列表进行"任意"操作.

一种可能的解决方案是将列表更改为可能是集合或其他东西,毕竟,我在其他地方使用linq查询加载它,但签名是什么?

das*_*ght 14

尝试Shift在查询中不使用s 集合的此更改:

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();
}
Run Code Online (Sandbox Code Playgroud)

我们的想法是避免Shift使用ID来将对象传递给查询提供程序.