在没有获得“仅支持初始化器,实体成员和实体导航属性”的.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

查看:118
本文介绍了在没有获得“仅支持初始化器,实体成员和实体导航属性”的.net MVC 4模型中的聚合字段。来自LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的答案引起了另外一个问题:当类被关联时,如何使用LINQ表达式作为查询中类的静态成员多次到第二班



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



该网站的核心实体是出售的物品,由几个不同的公司创建并分为几个类别。我的任务是允许每家公司为全球类别提供自己的可选别名。获取数据库和模型中设置的两个类别没有问题,使应用程序在存在时使用新的可选别名,默认为全局,否则我正在努力寻找最佳方法。



向每个LINQ查询添加一个合并语句将会显示出正常的效果,但是这个逻辑需要存在的地方有几十个,最好将这个逻辑保留在一个不可避免的地方



以下代码是我尝试在模型中存储合并,但这会导致仅初始化器,实体成员和实体导航属性被支持。执行LINQ查询时抛出的错误。我不确定我可以如何实现类似于LINQ友好的不同方法。



型号:

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

public String CompanyId {get;组; }

public Int32 CategoryId {get;组; }

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

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

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

控制器LINQ示例: p>

  var categories =(从我的db.Items 
其中i.CompanyId == siteCompanyId
orderby i。 PreferredCategoryName
选择i.PreferredCategoryName).Distinct();

var itemsInCategory =(从我在db.Items
其中i.CompanyId == siteCompanyId
&& i.PreferredCategoryName == categoryName
select i) ;


解决方案

对于一个您正在使用编译函数( getPreferredCategoryName ),除非EF知道如何翻译你有麻烦。



在项目定义中尝试以下内容:

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

使用如下:

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

这应该可以正常工作,因为您有一个通用的未编译的表达式树,EF可以解析。 >

The answer to this question gave rise to this other question: How to use LINQ expressions as static members of classes in queries when the class is related multiple times to a second 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.

这篇关于在没有获得“仅支持初始化器,实体成员和实体导航属性”的.net MVC 4模型中的聚合字段。来自LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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