[asp.net]实体框架可以在 linq 查询有列表对象时不生成表达式目录树

发布时间: 2016/11/13 3:20:14
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

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

'System.NotSupportedException' 类型的异常发生在 System.Data.Entity.dll 中,但不在用户代码中处理
附加信息︰ 无法创建类型常量值 'MvcApplication1.Models.QuantityPerUnit'。只有基元类型或枚举类型都支持在这方面。

它似乎像丹我不能使用 Listpublic static List<QuantityPerUnit> QuantityPerUnitItems = new List<QuantityPerUnit> 。它应该是替换 IEnumerable,但如果我用 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>

解决方法 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,
        };
赞助商