当linq查询具有列表对象时,实体框架无法生成表达式树

本文关键字:框架 表达式 实体 查询 linq 列表 对象 | 更新日期: 2023-09-27 18:19:05

我的代码如下:当我运行它时,我得到了一个异常:

类型为'System '的异常。在System.Data.Entity.dll中发生NotSupportedException',但未在用户代码中处理
附加信息:无法创建类型为"mvapplication1 . models . quantityperunit"的常量值。在此上下文中只支持基本类型或枚举类型。"

似乎我不能使用List inpublic static List<QuantityPerUnit> QuantityPerUnitItems = new List<QuantityPerUnit>。它应该替换为IEnumerable,但如果我使用IEnumerable,它显示比较错误:

System.Collections.Generic。IEnumerable'不包含'Add

的定义
代码:

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>

当linq查询具有列表对象时,实体框架无法生成表达式树

您需要将查询中希望转换为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,
        };

Matthew.

当你从数据库中获取项目时,你操作的类型是IQueryable(T)。这种类型被转换成SQL脚本。这种LINQ被称为-对实体的LINQ。

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

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