c# - LINQ to Entities 仅支持无参数构造函数和初始值设定项

标签 c# linq entity-framework linq-to-entities

在尝试使用半复杂查询在页面上显示一些 ListView 内容时,我遇到了著名的“LINQ to Entities 中仅支持无参数构造函数和初始化程序”错误。

这是我使用的代码......我找不到我在查询中使用参数初始化某些东西的地方....

protected void ArtistsList()
{
    Guid cat1 = new Guid("916ec8ae-8336-43b1-87c0-8536b2676560");
    Guid cat2 = new Guid("92f2a07f-0570-4521-870a-bf898d1e92d6");

    var memberOrders = (from o in DataContext.OrderSet
                        where o.Status == 1 || o.Status == 0
                        select o.ID);

    var memberOrderDetails = (from o in DataContext.OrderDetailSet
                              where memberOrders.Any(f => f == o.Order.ID)
                              select o.Product.ID );

    var inventoryItems = (from i in DataContext.InventoryItemSet
                          select i.Inventory.Product.ID);

    var products = (from p in DataContext.ProductSet
                    join m in DataContext.ContactSet on p.ManufacturerID equals m.ID
                    where p.Active == true
                       && p.ShowOnWebSite == true
                       && p.Category.ID != cat1
                       && p.Category.ID != cat2
                       && p.AvailableDate <= DateTime.Today
                       && (p.DiscontinuationDate == null || p.DiscontinuationDate >= DateTime.Today)
                       && memberOrderDetails.Any(f => f != p.ID)
                       && inventoryItems.Any(f => f == p.ID)
                    select new { ContactID = m.ID, ContactName = m.Name });

    artistsRepeater.DataSource = products;
    artistsRepeater.DataBind();

    Response.Write("PRODUCT COUNT: " + products.Count());
}

错误本身出现在行 artistsRepeater.DataSource = products;

我试图评论行 && memberOrderDetails.Any(f => f != p.ID)&& inventoryItems.Any(f => f == p.ID) ,仍然没有改变任何东西

有什么提示吗?

[编辑]

在 LINQpad 中,它与连接一起工作,但它在注释行上出现错误

(from p in Products
join m in Members on p.ManufacturerID.Value equals m.ID
where p.Active == true
&& p.ShowOnWebSite == true
&& p.AvailableDate <= DateTime.Today
&& (p.DiscontinuationDate == null || p.DiscontinuationDate >= DateTime.Today)
//&& (from od in MemberOrderDetails where (from mo in MemberOrders where mo.Status == 1 || mo.Status == 0 select mo.ID).Any(f => f == od.ID) select od.Product.ID)
&& (from inv in InventoryItems select inv.Inventory.ProductID).Any(i => i.Value == p.ID)
select m).Distinct()

[edit-2]

LINQpad 中的这个查询似乎没问题:

(from p in Products
join m in Members on p.ManufacturerID.Value equals m.ID
where p.Active == true
&& p.ShowOnWebSite == true
&& p.AvailableDate <= DateTime.Today
&& (p.DiscontinuationDate == null || p.DiscontinuationDate >= DateTime.Today)
&& !(from od in MemberOrderDetails where (from mo in MemberOrders where mo.Status == 1 || mo.Status == 0 select mo).Any(f => f.ID == od.ID) select od.Product.ID).Any(i => i == p.ID)
&& (from inv in InventoryItems select inv.Inventory.ProductID).Any(i => i.Value == p.ID) 
select m)

最佳答案

好的,这很微妙,但是如果您将 LINQPad 查询从以下位置更改为:

           (from p in Products
            join m in Members 
                on p.ManufacturerID.Value equals m.ID
            where p.Active == true
                && p.ShowOnWebSite == true
                && p.AvailableDate <= DateTime.Today
                && (p.DiscontinuationDate == null || p.DiscontinuationDate >= DateTime.Today)
                && (from od in MemberOrderDetails 
                    where (from mo in MemberOrders 
                           where mo.Status == 1 || mo.Status == 0 
                           select mo.ID).Any(f => f == od.ID) 
                    select od.Product.ID)
                && (from inv in InventoryItems 
                    select inv.Inventory.ProductID).Any(i => i.Value == p.ID)

...到:

           (from p in Products
            join m in Members 
                on p.ManufacturerID.Value equals m.ID
            where p.Active == true
                && p.ShowOnWebSite == true
                && p.AvailableDate <= DateTime.Today
                && (p.DiscontinuationDate == null || p.DiscontinuationDate >= DateTime.Today)
                && (from od in MemberOrderDetails 
                    where (from mo in MemberOrders 
                           where mo.Status == 1 || mo.Status == 0 
                           select mo).Any(f => f.ID == od.ID)          // NOTE!
                    select od.Product.ID)
                && (from inv in InventoryItems 
                    select inv.Inventory.ProductID).Any(i => i.Value == p.ID)

为什么?我认为类型推断可能在这里做错了。我在 DateTime 中看到过类似的情况。

关于c# - LINQ to Entities 仅支持无参数构造函数和初始值设定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2096159/

相关文章:

c# - 在 C# 中从 SQL 读取 BLOB 后转换为字节数组

c# - 在 C# 中使用泛型参数调用委托(delegate)

c# - 无法加载文件或程序集 'System.Private.ServiceModel,版本 = 4.1.2.4

c# - 避免在 native 到托管回调上分配字符串

c# - 从 Entity Framework 调用时出现存储过程错误

c# - 设置包含对其他复杂对象的循环引用的复杂对象以进行单元测试

c# - 表达式树可能不包含赋值运算符?

c# - 表达式 <Func<object, bool>> 作为属性

c# - LINQ 链接中的 Any() 运算符

c# - 如何使用 Entity Framework 在上下文之外访问实体的属性?