如何在LINQ查询中指定范围变量的类型?

Lui*_*cio 8 c# linq winforms

如何在linq查询中指定范围变量的类型?

Jon*_*eet 16

只需用变量本身声明它:

var query = from string text in collection
            where text.Length > 5
            select text.ToUpper();
Run Code Online (Sandbox Code Playgroud)

这将转化为:

var query = collection.Cast<string>()
                      .Where(text => text.Length > 5)
                      .Select(text => text.ToUpper());
Run Code Online (Sandbox Code Playgroud)