类成员变量和"Queries with local collections are not supported"



我有一个List作为IN操作符的源。典型的linq到sql查询:

public void foo()
{
   var inThisListOnly = new string[] { "VA", "MA" };
   var result = (
       from o in Orders
       where inThisListOnly.Contains(o.State)
       select o
   )
   .ToDictionary( o => o.Id, o => o );
}

一切正常。我想通过将inThisListOnly移到foo之外以成为类的成员变量来节省一些CPU周期。

public class FooClass
{
    private readonly string[] _inThisListOnly = new string[] { "VA", "MA" };
    public void foo()
    {
        var result = (
          from o in Orders
          where _inThisListOnly.Contains(o.State)
          select o
        )
        .ToDictionary( o => o.Id, o => o );
    }
}

修改后,我的程序开始抛出臭名昭著的"不支持本地集合查询"异常。有人能解释一下这种行为吗?在某种意义上,是不是_inThisListOnly比inThisListOnly更少的本地?

谢谢。

Order集合的类型是什么?它是本地可枚举的还是linq到sql提供的表?

相关内容

最新更新