仅实体类型,枚举类型或原始类型在这种情况下,支持 [英] Only entity types, enumeration types or primitive types are supported in this context

查看:123
本文介绍了仅实体类型,枚举类型或原始类型在这种情况下,支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个搜索页面上。我只需要返回包含所有themetag的ID存储在中的int [] ST的主题themedetails的列表。目前该行(ST == NULL真:?ST.Contains(b.ThemeTagID))似乎给我一个错误



信息:无法创建一个空不变值的类型'System.Int32 []'。只有实体类型,枚举类型或基本类型在这方面的支持。

 公众的ActionResult指数(INT ProviderID = 0,字符串。 = NULL,INT [] ST = NULL)
{

VAR themedetail =从db.ThemeDetail $ b $(b T)从b在t.ThemeTags
,其中(
(?string.IsNullOrEmpty(描述)真:t.Description.ToLower()包含(Description.ToLower()))及和放大器;
(ProviderID == 0真:?t.ProviderID == ProviderID)及和放大器;
(ST == NULL真:?ST.Contains(b.ThemeTagID))

选择吨;

ViewBag.ProviderID =新的SelectList(db.ProviderDetails,ProviderID,的ProviderName);
ViewBag.MultiselectFeatures =新MultiSelectList(db.ThemeFeatures,ThemeFeatureID,ThemeFeaturesName);
ViewBag.MultiselectTags =新MultiSelectList(db.ThemeTags,ThemeTagID,变量名);

返回查看(themedetail.ToList());
}



该机型...

  [表(ThemeDetail)] 
公共类ThemeDetail:实体
{
[必填]
[显示( NAME =名称)]
公共字符串THEMENAME {搞定;组; }
公共ThemeDetail()
{
ThemeFeatures =新的List< ThemeFeature>();
ThemeTags =新的List< ThemeTag>();
ThemeImages =新的List< ThemeImage>();
}

公共虚拟的ICollection< ThemeFeature> ThemeFeatures {搞定;组; }
公共虚拟的ICollection< ThemeTag> ThemeTags {搞定;组; }
公共虚拟的ICollection< ThemeImage> ThemeImages {搞定;组; }
}

表(ThemeTags)]
公共类ThemeTag
{
[关键]
[显示(名称= 主题标签ID)]
公众诠释ThemeTagID {搞定;组; }

[显示(NAME =标签名称)]
[必填]
公共字符串标记名{获得;组; }

公共虚拟的ICollection< ThemeDetail> ThemeDetail {搞定;组; }
}


解决方案

您正在使用 ST 查询内,但它不能转换为SQL,因为ST是 INT [] 键,可)和SQL没有任何概念的阵列



如果您改变查询,以避免检查EF将能够使用翻译该查询 WHERE ThemeTagID IN(...)与价值观从你的列表中提供(要小心,如果列表可能来自另一个查询与成千上万的元素):

 公众的ActionResult指数(INT ProviderID = 0 ... 
{
如果(ST == NULL)
ST = INT新[0];

然后,只需更改此:

 (ST == NULL真:?ST 。载有(b.ThemeTagID))

要这样:

  ST.Contains(b.ThemeTagID)


I'm currently working on a search page. I need to only return a list of themedetails for themes that contain all of the themetag id's that are stored in the int[] ST. Currently the line (ST == null ? true : ST.Contains(b.ThemeTagID)) seems to give me an error

Additional information: Unable to create a null constant value of type 'System.Int32[]'. Only entity types, enumeration types or primitive types are supported in this context.

    public ActionResult Index(int ProviderID = 0, string Description = null, int[] ST = null)
    {

        var themedetail = from t in db.ThemeDetail
                          from b in t.ThemeTags
                          where (
                          (string.IsNullOrEmpty(Description) ? true : t.Description.ToLower().Contains(Description.ToLower())) &&
                          (ProviderID == 0 ? true : t.ProviderID == ProviderID) &&
                          (ST == null ? true : ST.Contains(b.ThemeTagID))
                              )
                          select t;

        ViewBag.ProviderID = new SelectList(db.ProviderDetails, "ProviderID", "ProviderName");
        ViewBag.MultiselectFeatures = new MultiSelectList(db.ThemeFeatures, "ThemeFeatureID", "ThemeFeaturesName");
        ViewBag.MultiselectTags = new MultiSelectList(db.ThemeTags, "ThemeTagID", "TagName");

        return View(themedetail.ToList());
    }

The Models are...

[Table("ThemeDetail")]
public class ThemeDetail : Entity
{
    [Required]
    [Display(Name = "Name")]
    public string ThemeName { get; set; }
    public ThemeDetail()
    {
        ThemeFeatures = new List<ThemeFeature>();
        ThemeTags = new List<ThemeTag>();
        ThemeImages = new List<ThemeImage>();
    }

    public virtual ICollection<ThemeFeature> ThemeFeatures { get; set; }
    public virtual ICollection<ThemeTag> ThemeTags { get; set; }
    public virtual ICollection<ThemeImage> ThemeImages { get; set; }
}

[Table("ThemeTags")]
public class ThemeTag
{
    [Key]
    [Display(Name = "Theme Tag ID")]
    public int ThemeTagID { get; set; }

    [Display(Name = "Tag Name")]
    [Required]
    public string TagName { get; set; }

    public virtual ICollection<ThemeDetail> ThemeDetail { get; set; }
}

解决方案

You're using ST inside your query but it can't be translated to SQL because ST is int[] and can be null) and in SQL there isn't any notion of array.

If you change your query to avoid null checking EF will be able to translate that query using WHERE ThemeTagID IN (...) with values provided from your list (be careful if list may come from another query with thousands of elements):

public ActionResult Index(int ProviderID = 0...
{
    if (ST == null)
        ST = new int[0];

Then simply change this:

(ST == null ? true : ST.Contains(b.ThemeTagID))

To this:

ST.Contains(b.ThemeTagID)

这篇关于仅实体类型,枚举类型或原始类型在这种情况下,支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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