【问题标题】:Entity Framework can not generate a expression tree when linq query has List object当 linq 查询具有 List 对象时,Entity Framework 无法生成表达式树
【发布时间】:2014-10-16 17:23:00
【问题描述】:

我有下面的代码。当我运行它时,我得到了异常:

System.Data.Entity.dll 中出现“System.NotSupportedException”类型的异常,但未在用户代码中处理
附加信息:无法创建“MvcApplication1.Models.QuantityPerUnit”类型的常量值。此上下文仅支持原始类型或枚举类型。”

好像我不能在里面使用List public static List<QuantityPerUnit> QuantityPerUnitItems = new List<QuantityPerUnit>。它应该替换为 IEnumerable,但如果我使用 IEnumerable,它会显示比较错误:

System.Collections.Generic.IEnumerable' 不包含“添加”的定义

代码:

namespace MvcApplication1.Models
{
    public class ProductEditViewModel : Product
    {
        // For DropDownListFor need IEnumerable<SelectListItem>
        public IEnumerable<SelectListItem> SupplierItems { get; set; }

        // For RadioButtonFor need below
        public Int32? CategorySelectedId { get; set; }
        public IEnumerable<Category> CategorieItems { get; set; }

        // For CheckBoxFor need below
        public string QuantityPerUnitSelectedId { get; set; }
        public IEnumerable<QuantityPerUnit> QuantityPerUnitItems { get; set; }
    }

    public class QuantityPerUnit
    {
        public string QuantityPerUnitId { get; set; }
        public string Quantity { get; set; }

        public static List<QuantityPerUnit> QuantityPerUnitItems = new List<QuantityPerUnit>
        {
            new QuantityPerUnit { QuantityPerUnitId = "1", Quantity = "10" },
            new QuantityPerUnit { QuantityPerUnitId = "2", Quantity = "20" },
            new QuantityPerUnit { QuantityPerUnitId = "3", Quantity = "25" },
            new QuantityPerUnit { QuantityPerUnitId = "4", Quantity = "50" },
            new QuantityPerUnit { QuantityPerUnitId = "5", Quantity = "100" }
        };
    }
}


[HttpGet]
public ActionResult ProductEdit(Int32 ProductId)
{
    var northwind = new NorthwindEntities();

    var q = from p in northwind.Products
            where p.ProductID == ProductId
            select new ProductEditViewModel
            {
                ProductID = p.ProductID,
                ProductName = p.ProductName,
                UnitPrice = p.UnitPrice,
                Discontinued = p.Discontinued,

                SupplierItems = from sup in northwind.Suppliers
                                select new SelectListItem
                                    {
                                        Text = sup.CompanyName,
                                        Value = SqlFunctions.StringConvert((double)sup.SupplierID),
                                        Selected = sup.SupplierID == p.SupplierID
                                    },

                CategorySelectedId = p.CategoryID,
                CategorieItems = from cat in northwind.Categories select cat,

                QuantityPerUnitSelectedId = p.QuantityPerUnit,
                QuantityPerUnitItems = QuantityPerUnit.QuantityPerUnitItems
            };

    return View(q.SingleOrDefault());
}


<div class="form-group">
    @Html.LabelFor(model => model.QuantityPerUnit)
    @foreach(var Quantity in MvcApplication1.Models.QuantityPerUnit.QuantityPerUnitItems)
    {
        @Html.CheckBoxFor(model => model.QuantityPerUnitSelectedId == Quantity.QuantityPerUnitId, Quantity.QuantityPerUnitId)
    }
</div>

【问题讨论】:

    标签: c# asp.net asp.net-mvc entity-framework linq-to-entities


    【解决方案1】:

    您需要将要转换为 SQL 并由数据库执行的查询部分与要在应用程序端完成的部分分开。

    var q = (from p in northwind.Products
            where p.ProductID == ProductId
            select new ProductEditViewModel
            {
                ProductID = p.ProductID,
                ProductName = p.ProductName,
                UnitPrice = p.UnitPrice,
                Discontinued = p.Discontinued,
    
                SupplierItems = from sup in northwind.Suppliers
                                select new SelectListItem
                                    {
                                        Text = sup.CompanyName,
                                        Value = SqlFunctions.StringConvert((double)sup.SupplierID),
                                        Selected = sup.SupplierID == p.SupplierID
                                    },
    
                CategorySelectedId = p.CategoryID,
                CategorieItems = from cat in northwind.Categories select cat,
    
                QuantityPerUnitSelectedId = p.QuantityPerUnit,
                //remove this from here
                //QuantityPerUnitItems = QuantityPerUnit.QuantityPerUnitItems
            })
            .AsEnumerable()
            .Select(p => new ProductEditViewModel
            {
                p.ProductID,
                //all of the other properties 
                QuantityPerUnitItems = QuantityPerUnit.QuantityPerUnitItems,
            };
    

    【讨论】:

      【解决方案2】:

      马修。

      当您从数据库中获取项目时,您使用 IQueryable(T) 类型进行操作。 这种类型被翻译成 SQL 脚本。 这种 LINQ 称为 - Linq to entity。

      要执行您的操作,您需要使用 Linq to Objects。

      您需要借助 AsEnumerable() 方法将查询转换为 IEnumerable(T),然后将静态数据添加到您的 ViewModel。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-29
        • 1970-01-01
        • 1970-01-01
        • 2020-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多