【问题标题】:Unable to create a constant value of type 'Closure type'无法创建“闭包类型”类型的常量值
【发布时间】:2011-02-17 21:12:39
【问题描述】:

这是我的 linq to entity 查询,我收到了这个错误

“无法创建‘闭包类型’类型的常量值。在此上下文中仅支持原始类型(‘例如 Int32、String 和 Guid’)。 "

有没有人知道如何解决它或解决方法。

PS。我使用的是 Linq to Entity 而不是 Linq to SQL

List<int> listint
List<int> listintTwo

  return (from oa in _entity.TableOne
          join cc in _entity.TableTwo on oa.TableSix.ColumnOne equals cc.TableSix.ColumnOne
          join os in _entity.TableThree on oa.TableThree.ColumnTwo equals os.ColumnTwo
          join cs in _entity.TableTwotatus on cc.TableTwotatus.ColumnThree equals cs.ColumnThree
          join app in _entity.TableFour on cc.TableFour.ColumnFour equals app.ColumnFour
          join cl in _entity.TableFive on app.TableFive.ColumnFive equals cl.ColumnFive
          where listint.Any(x =>x == cc.TableTwotatus.ColumnThree)
          && listintTwo.Any(x => x == os.ColumnTwo) && cc.TableSix.ColumnOne == ColumnOne 
          select new TableFive {ColumnFive = cl.ColumnFive, CompanyName = cl.CompanyName}).ToList();

【问题讨论】:

    标签: c# linq-to-entities


    【解决方案1】:

    尝试将您的呼叫更改为 Any(...)Contains(...)

    where listint.Contains(cc.TableTwotatus.ColumnThree)
    && listintTwo.Contains(os.ColumnTwo) && ...
    

    我很确定您从 lambda 表达式(充当闭包)引用连接实体时遇到问题。

    【讨论】:

      【解决方案2】:

      您不能查询具有复杂类型的实体。因此,首先从 EF 获取结果,然后加入您的复杂类型。

      List<int> listint
      List<int> listintTwo
      
      return (from oa in _entity.TableOne
            join cc in _entity.TableTwo on oa.TableSix.ColumnOne equals cc.TableSix.ColumnOne
            join os in _entity.TableThree on oa.TableThree.ColumnTwo equals os.ColumnTwo
            join cs in _entity.TableTwotatus on cc.TableTwotatus.ColumnThree equals cs.ColumnThree
            join app in _entity.TableFour on cc.TableFour.ColumnFour equals app.ColumnFour
            join cl in _entity.TableFive on app.TableFive.ColumnFive equals cl.ColumnFive).ToList().
            where listint.Any(x =>x == cc.TableTwotatus.ColumnThree)
            && listintTwo.Any(x => x == os.ColumnTwo) && cc.TableSix.ColumnOne == ColumnOne 
            select new TableFive {ColumnFive = cl.ColumnFive, CompanyName = cl.CompanyName}).ToList();</pre>
      

      【讨论】:

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