将一段代码转换为LINQ(短语法)

时间:2010-10-02 05:44:45

标签: linq

我想转换此LINQ代码

var x = from nm in names 
        select MyClass.SomeMethod(nm).TrimStart(',');
foreach (var vv in x)
{
    // I want to group and count different types of vv here
}

使用较短的语法,其中x => LINQ中的x。我也想分组并计算'vv'(可能有很多类似的vv)

2 个答案:

答案 0 :(得分:3)

嗯,上面的“点符号”或“流利的符号”是:

var x = names.Select(nm => MyClass.SomeMethod(nm).TrimStart(','));

对于分组:

var x = names.Select(nm => MyClass.SomeMethod(nm).TrimStart(','));
             .GroupBy(vv => vv, 
                      (key, group) => new { Key = key, Count = group.Count() });

答案 1 :(得分:0)

这样的东西?

MyClass.SomeMethod(names).TrimStart(',')
.GroupBy(x => x.vv)
.ToList()
.ForEach(x => Console.WriteLine(x.Key + ": " + x.Count()));