合并没有得到&QUOT在.NET MVC 4模型领域;只有初始化,实体成员和实体导航属性都支持"从LINQ [英] Coalesce fields in a .net MVC 4 model without getting "Only initializers, entity members, and entity navigation properties are supported" from LINQ

查看:124
本文介绍了合并没有得到&QUOT在.NET MVC 4模型领域;只有初始化,实体成员和实体导航属性都支持"从LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回答这个问题引起了这个其他问题:如何使用静态LINQ前pressions因为多次的方法类相关第二模型类

我有一个现有ASP.net MVC 4个网站,我需要修改。

本网站内的核心实体是项目是出售,这是由几个不同的公司创建并分成几类。我的任务是让每个公司自己的可选别名为全球类别。获取两类数据库和模型建立是没有问题的,使得应用程序使用新的可选别名时,它的存在和默认的全球不然就是我苦苦寻找的最佳方法。

添加COALESCE语句,每一个LINQ查询将清楚的工作,但在有些情况下这种逻辑就需要有几十个位置,这将是preferable把这个逻辑在一个地方时,不可避免的变化来。

以下code是我试图存储在模型中聚结,而这会导致只有初始化,实体成员和实体导航属性都支持。抛出错误当执行LINQ查询。我不确定,我怎么能实现用不同的方法,那就是更多的LINQ友好类似的东西。

型号:

 公共类项目
{
    [StringLength(10)]
    [键]
    公共字符串ITEMID {搞定;组; }    公共字符串CompanyId {搞定;组; }    公众的Int32的CategoryId {搞定;组; }    [ForeignKey的(类别ID)]
    公共虚拟GlobalCategory GlobalCategory {搞定;组; }    [ForeignKey的(CompanyId,类别ID)]
    公共虚拟CompanyCategory CompanyCategory {搞定;组; }    公共字符串preferredCategoryName
    {
        得到{
            回报(CompanyCategory.CategoryAlias​​ == NULL || CompanyCategory.CategoryAlias​​ ==)? GlobalCategory.CategoryName:CompanyCategory.CategoryAlias​​;
        }
    }
}

控制器LINQ的例子:

  VAR类=(从我在db.Items
                          其中,i.CompanyId == siteCompanyId
                          排序依据我。preferredCategoryName
                          选择我preferredCategoryName).Distinct()。        在db.Items VAR itemsInCategory =(从我
                          其中,i.CompanyId == siteCompanyId
                          &功放;&安培;我。preferredCategoryName ==类别名称
                          选择我);


解决方案

有关一个你正在使用的查询编译功能(的get preferredCategoryName ),除非EF知道怎么翻译,你有麻烦了。

请尝试以下方法在项目定义:

 公共静态防爆pression<&Func键LT;项目,字符串>> preferredCategoryName
{
    得到
    {
        返回I => (i.CompanyCategory.CategoryAlias​​ == NULL || i.CompanyCategory.CategoryAlias​​ ==)?
                     i.GlobalCategory.CategoryName:
                     i.CompanyCategory.CategoryAlias​​;
    }
}

哪个的使用如下:

  VAR类别= db.Items.Where(I => i.CompanyID == siteCompanyId)
                         .OrderBy(项目。preferredCategoryName)
                         。选择(项目。preferredCategoryName)
                         。不同();

这应该工作,你有一个可用的一般前未编译pression树EF可然后解析。

The answer to this question gave rise to this other question: How to use static LINQ expressions as method of classes related multiple times to a second model class

I have an existing ASP.net MVC 4 site which I need to modify.

The core entity within this site are Items that are for sale, which are created by several different companies and divided into several categories. My task is to allow each company its own optional alias for the global categories. Getting the two categories set up in the database and model was no problem, making the application use the new optional alias when it exists and default to the global otherwise is where I'm struggling to find the optimal approach.

Adding a coalesce statement to every LINQ query will clearly work, but there are several dozen locations where this logic would need to exist and it would be preferable to keep this logic in one place for when the inevitable changes come.

The following code is my attempt to store the coalesce in the model, but this causes the "Only initializers, entity members, and entity navigation properties are supported." error to be thrown when the LINQ query is executed. I'm unsure how I could achieve something similar with a different method that is more LINQ friendly.

Model:

public class Item
{
    [StringLength(10)]
    [Key]
    public String ItemId { get; set; }

    public String CompanyId { get; set; }

    public Int32 CategoryId { get; set; }

    [ForeignKey("CategoryId")]
    public virtual GlobalCategory GlobalCategory { get; set; }

    [ForeignKey("CompanyId, CategoryId")]
    public virtual CompanyCategory CompanyCategory { get; set; }

    public String PreferredCategoryName
    { 
        get{
            return (CompanyCategory.CategoryAlias == null || CompanyCategory.CategoryAlias == "") ? GlobalCategory.CategoryName : CompanyCategory.CategoryAlias;
        }
    }
}

Controller LINQ examples:

       var categories = (from i in db.Items
                          where i.CompanyId == siteCompanyId
                          orderby i.PreferredCategoryName
                          select i.PreferredCategoryName).Distinct();

        var itemsInCategory = (from i in db.Items
                          where i.CompanyId == siteCompanyId
                          && i.PreferredCategoryName == categoryName
                          select i);

解决方案

For one you are using a compiled function (getPreferredCategoryName) in the query, unless EF knows how to translate that you are in trouble.

Try the following in item definition:

public static Expression<Func<Item,String>> PreferredCategoryName
{
    get
    {
        return i => (i.CompanyCategory.CategoryAlias == null || i.CompanyCategory.CategoryAlias == "") ? 
                     i.GlobalCategory.CategoryName : 
                     i.CompanyCategory.CategoryAlias;
    }
}

Which is used as follows:

var categories = db.Items.Where(i => i.CompanyID == siteCompanyId)
                         .OrderBy(Item.PreferredCategoryName)
                         .Select(Item.PreferredCategoryName)
                         .Distinct();

This should work as you have a generically available uncompiled expression tree that EF can then parse.

这篇关于合并没有得到&QUOT在.NET MVC 4模型领域;只有初始化,实体成员和实体导航属性都支持&QUOT;从LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆