gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:51:10 26 4
gpt4 key购买 nike

在尝试使用半复杂查询在页面上显示一些 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com