【问题标题】:Unable to construct an entity (complex) object from a query无法从查询构造实体(复杂)对象
【发布时间】:2010-06-08 21:15:35
【问题描述】:

嘿,我对实体、sql、c# 和 asp.net 很陌生,所以这可能很容易解决。

我正在尝试在数据网格中显示存储在我的名为 products 的表中的所有非活动产品。

var productQuery = from b in solutionContext.Version
                   where b.Product.Name == search && b.Product.ActiveNumber > b.VersionNumber
                   select new Product
                   {
                                   Name = b.Product.Name,
                                   Description = b.Product.Description,
                                   ID = b.ID,
                                   LastNumber = b.Product.LastNumber,
                                   MiddleNumber = b.Product.MiddleNumber,
                                   RSTATE = b.RSTATE,
                                   ActiveNumber = b.Product.ActiveNumber,
                                   LastModified = b.Product.LastModified,
                                   ParentID = b.Product.ParentID,
                                   ProductType = b.Product.ProductType
                  };

                  ProductsGrid.DataSource = productQuery;
                  ProductsGrid.DataBind();

我收到此错误:

  • $exception {"无法在 LINQ to Entities 查询中构造实体或复杂类型 'SolutionsModel.Product'。"} System.Exception {System.NotSupportedException}

感谢您的建议!

【问题讨论】:

    标签: c# asp.net sql-server entity-framework


    【解决方案1】:

    错误是说您无法生成 Product 类型的 LINQ to Entities 对象。但是你可以退货。您可以尝试退回可能正常工作的 b.Product 吗?

    var productQuery = from b in solutionContext.Version
                       where b.Product.Name == search && b.Product.ActiveNumber > b.VersionNumber
                       select b.Product; 
    
                      ProductsGrid.DataSource = productQuery;
                      ProductsGrid.DataBind();
    

    【讨论】:

    • 谢谢,但是我应该把退货放在哪里?抱歉,我仍在努力解决这个问题。
    • 非常感谢您,先生。我确信在这个项目的生命周期中我会有更多的问题,但这是一个非常有用的步骤。祝你有美好的一天
    • 哦,如果你不介意我有一个侧面问题。每个非活动产品都有不同的版本,具有不同的 RSTATE。这些存储在我的版本表中。由于我返回 Product 对象,我需要将它们的 RSTATE 更改为与它们相关的 Version 对象相同。从产品到版本之间存在一对多的关系。我如何访问这些并更改我退回的产品的这些属性。我认为它会像
    • foreach (var product in productQuery) { product.RSTATE = product.Versions.RSTATE;但显然不是这样,我想我不知道一旦查询完成,如您向我展示的那样,我不知道如何在这种一对多关系上倒退。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    相关资源
    最近更新 更多