2010-06-25 198 views
0

我认为有一个linq预处理器可以将你的linq表达式预处理为常规的c#语句,如.Select().Group()等,这对于学习幕后的内容很有用。 linq,学习linq或调试复杂的linq表达式。这样的工具或设施是否存在?我无法在LinqPad中找到它。Linq预处理器?

回答

3

要在LINQPad中使用此功能,请运行查询,然后单击结果窗口上的lambda按钮。请注意,这仅适用于基于IQueryable的查询。这意味着,对于本地查询,则必须调用.AsQueryable():

from n in new[] { "Tom", "Dick", "Harry" }.AsQueryable() 
where n.Contains ("a") 
select n 

从查询表达式到精通语法翻译是有多个发电机查询特别有意思,加入或let语句。例如:

var fullNames = new[] { "Anne Williams", "John Fred Smith", "Sue Green" }.AsQueryable(); 

IEnumerable<string> query = 
    from fullName in fullNames 
    from name in fullName.Split() 
    orderby fullName, name 
    select name + " came from " + fullName; 

query.Dump(); 

这相当于:

System.String[] 
    .SelectMany (
     fullName => fullName.Split (new Char[0]), 
     (fullName, name) => 
     new 
     { 
      fullName = fullName, 
      name = name 
     } 
    ) 
    .OrderBy (temp0 => temp0.fullName) 
    .ThenBy (temp0 => temp0.name) 
    .Select (temp0 => ((temp0.name + " came from ") + temp0.fullName)) 
1

Resharper可以将LINQ表达式转换为方法链。

1

您也可以尝试通过Reflector运行Linq表达式(具体请参见投诉2)。