我目前正在搜索搜索页面。我只需返回包含存储在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; }
}
答案 0 :(得分:5)
您在查询中使用了ST
,但它无法转换为SQL,因为ST是int[]
并且可以是null
)而在SQL中没有&{&} #39; array 的任何概念。
如果您更改查询以避免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)