使用linq投影在where子句中包装匿名类型

时间:2015-07-16 19:40:20

标签: c# linq

我最近发现自己正在使用Linq Projection,并且有点想知道是否有可能我可以采用我的匿名类型并将它们包含在不同类型的子句中。

例如:

var projection = (from m in myData
             select new 
             {
                 // I would like to wrap my where around this instead of at the end of
                 // var projection so that i can have multiple types that run differnt
                 // linq queries
                 ServiceSelector = new
                 {
                     // I know i could have the Where() after each property, but i would 
                     // rather not have it query for each one
                     ServiceOne = m.ServiceOne,
                     ServiceTwo = m.ServiceTwo,
                     ServiceThree = m.ServiceThree
                 }
                 // end wrap of Where() here...
             }).FirstOrDefault();
             // I would also not like to have it here. Since I may have other anonymous
             // types that dont need it

这样我可以打电话:

ServiceOne = Projection.ServiceSelector.ServiceOne;

但是如果我可以调用另一个使用GroupBy()的匿名类型,而不是这样:

GroupOne = Projection.GroupSelector.GroupOne;

我希望有人可以提供更多的知识,因为我认为在其他情况下使用它真的很好。

0 个答案:

没有答案