开发者

class member variable and "Queries with local collections are not supported"

开发者 https://www.devze.com 2023-03-21 17:59 出处:网络
I have a List that serves as the source of the IN operator.The typical linq-to-sql query: public void foo()

I have a List that serves as the source of the IN operator. The typical linq-to-sql query:

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 );
}

everything works fine. I want to save a few CPU cycles by moving inThisListOnly outside of foo开发者_开发技巧 to become a member variable of the class.

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 );
    }
}

After the modifiction, my program starts to throw the notorious "Queries with local collections are not supported" exception. Would anyone provide an explanation to the behavior? In some sense, isn't _inThisListOnly less local than inThisListOnly?

Thanks.


What is the type of Order collection? Is it local enumerable or linq to sql provided table?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号