Performing part of a IQueryable query and deferring the rest to Linq for Objects

前端 未结 5 1026
温柔的废话
温柔的废话 2021-02-15 15:37

I have a Linq provider that sucessfully goes and gets data from my chosen datasource, but what I would like to do now that I have my filtered resultset, is allow Linq to Objects

5条回答
  •  梦毁少年i
    2021-02-15 15:48

    Both of the previous answers work, but it reads better if you use AsEnumerable() to cast the IQueryable to IEnumerable:

    // Using Bob's code...
    var result = datacontext.Table
       .Where(x => x.Prop == val)
       .OrderBy(x => x.Prop2)
       .AsEnumerable()  //  <---- anything after this is done by LINQ to Objects
       .Select(x => new { CoolProperty = x.Prop, OtherProperty = x.Prop2 });
    

    EDIT:

    // ... or MichaelGG's
    var res = dc.Foos
               .Where(x => x.Bla > 0)  // uses IQueryable provider
               .AsEnumerable()
               .Where(y => y.Snag > 0); // IEnumerable, uses LINQ to Objects
    

提交回复
热议问题