我目前正在开发一个搜索页面。我只需要返回主题的主题详细信息列表,其中包含存储在 int[] ST 中的所有主题标签 id。目前, (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<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 中没有任何概念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))
To this:
ST.Contains(b.ThemeTagID)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)