开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜