在此上下文中仅支持实体类型、枚举类型或基元类型
本文关键字:类型 枚举 实体 上下文 支持 | 更新日期: 2023-09-27 18:00:51
我目前正在处理一个搜索页面。我只需要返回主题的主题详细信息列表,这些主题包含存储在int[]ST中的所有主题标签id。目前,行(ST==null?true:ST.Contains(b.ThemeTagID((似乎给了我一个错误
其他信息:无法创建类型为"System.Int32[]"的null常数值。此上下文中只支持实体类型、枚举类型或基元类型。
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<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; }
}
您在查询中使用ST
,但它无法转换为SQL,因为ST是int[]
,可以是null
(,并且在SQL中没有任何数组的概念。
如果您更改查询以避免null
检查,EF将能够使用列表中提供的值使用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)