无法创建“闭包类型”类型的常量值

时间:2011-02-17 21:12:39

标签: c# linq-to-entities

这是我的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();

2 个答案:

答案 0 :(得分:1)

尝试将您的来电更改为Any(...)Contains(...)

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

我很确定你在lambda表达式中引用一个连接实体时会遇到问题(充当闭包)。

答案 1 :(得分:0)

您无法查询具有复杂类型的实体。首先从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>