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

时间:2010-01-19 18:55:03

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

尝试使用sem-complex查询在页面上显示一些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()

[编辑-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)

3 个答案:

答案 0 :(得分:1)

最可能的罪魁祸首是:

select new { ContactID = m.ID, ContactName = m.Name }

这是因为匿名类型没有无参数构造函数。有点奇怪的是,匿名类型在LINQ to Entities中是非常严格的。我只是没有看到任何可能冒犯的其他行。

首先尝试删除该行并查看错误是否消失。至少我们会知道它是否是那条线。然后我们可以集中精力找出原因。

修改:OrderSet.IDProduct.IDOrder.ID以及ContactSet.ID的类型有哪些?是否有任何Guid和隐式Guid构造函数被调用?

答案 1 :(得分:1)

好的,这很微妙,但如果您从以下位置更改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 s类似的事情。

答案 2 :(得分:0)

首先转换为列表,然后调用select语句:

var res = abc.getEmployess().toList().select(x => new keyvaluepair<int, string>(x.EmpID, x.EmpName.tostring)).tolist();