代码之家  ›  专栏  ›  技术社区  ›  Erick

LINQ to实体中只支持无参数构造函数和初始值设定项

  •  2
  • Erick  · 技术社区  · 15 年前

    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) ,仍然没有改变任何事情

    [编辑]

    (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()
    

    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 回复  |  直到 15 年前
        1
  •  1
  •   Craig Stuntz    15 年前

    好的,这很微妙,但是如果将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

        2
  •  1
  •   jason    15 年前

    最可能的罪魁祸首是:

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

    这是因为匿名类型没有无参数构造函数。奇怪的是,在LINQ中,匿名类型是对实体的定义。我只是看不到任何其他可能冒犯你的话。

    编辑:什么类型的文件 OrderSet.ID , Product.ID Order.ID ContactSet.ID ? 有吗 Guid 指南

        3
  •  0
  •   michaelb958--GoFundMonica Mong Tararak Ruchirabha    11 年前

    首先转换为列表,然后调用您的 select 声明:

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