在此上下文中仅支持实体类型,枚举类型或基元类型

我目前正在搜索搜索页面。 我只需要返回包含存储在int [] ST中的所有主题标签id的主题的themedetails列表。 目前该行(ST == null?true:ST.Contains(b.ThemeTagID))似乎给我一个错误

附加信息:无法创建类型为“System.Int32 []”的空常量值。 在此上下文中仅支持实体类型,枚举类型或基元类型。

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()); } 

模特是……

 [Table("ThemeDetail")] public class ThemeDetail : Entity { [Required] [Display(Name = "Name")] public string ThemeName { get; set; } public ThemeDetail() { ThemeFeatures = new List(); ThemeTags = new List(); ThemeImages = new List(); } public virtual ICollection ThemeFeatures { get; set; } public virtual ICollection ThemeTags { get; set; } public virtual ICollection 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 { get; set; } } 

你在查询中使用ST但是它不能被转换为SQL,因为ST是int[]并且可以是null )并且在SQL中没有任何数组的概念。

如果您更改查询以避免null检查EF将能够使用WHERE ThemeTagID IN (...)将您的查询转换为列表中提供的值(如果列表可能来自具有数千个元素的另一个查询, WHERE ThemeTagID IN (...)小心):

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

然后只需更改:

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

对此:

 ST.Contains(b.ThemeTagID)