在此上下文中,仅支持实体类型、枚举类型或原始类型。

3
我目前正在开发一个搜索页面。我需要返回一个只包含所有主题标签ID存储在int[] ST中的主题详情列表。目前,此行代码(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());
    }

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; }
}
1个回答

5
您在查询中使用了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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接