【问题标题】:Unable to create a constant value. Only primitive types无法创建常量值。只有原始类型
【发布时间】:2013-01-25 15:47:32
【问题描述】:
dbEntities db = new dbEntities();
foreach (ttCategory c in db.ttCategories)
{
    var tags=(from t in db.ttproduktes where t.ttCategories.Contains(c) select t.ttTags);
    foreach (ttTag t in tags)  // here it says:
                               // Unable to create a constant value - only primitive types
    {
       t.ToString();
    }
}

我做错了什么?

【问题讨论】:

  • 请提供有关您的架构的更多信息(最好更改您的类型名称以遵循正常的 .NET 命名约定)。
  • 似乎 ttTag(在 foreach 中)不是有效类型。
  • "ttCategory" 和 "ttTag" 是你的类名吗?在 foreach 中将其替换为“var”。
  • 这可能是相关的? stackoverflow.com/questions/879411/…

标签: c# linq entity-framework


【解决方案1】:

在 linq-to-entities 中,不能将 Contains 与类一起使用,只能将其与原始类型一起使用,因此需要更改:

where t.ttCategories.Contains(c)

 where t.ttCategories.Any(x => x.UniqueProperty == c.UniqueProperty)

【讨论】:

  • 现在我得到了 System.Collections.Generic.HashSet1[WT.Models.ttTag]" 无法转换为 Typ "WT.Models.ttTag"`的错误消息`
【解决方案2】:
var tags = (from t in db.ttproduktes
            where t.ttCategories.Any(q => q.Id == c.Id)
            select t.ttTags);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-16
    相关资源
    最近更新 更多