开发者

Convert piece of code into LINQ (short syntax)

I want to convert this开发者_运维问答 LINQ code

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
}

to use shorter syntax, one where they do x => x in LINQ. I also want to group and count 'vv' (there could be number of similar vv's)


Well, the "dot notation" or "fluent notation" for the above is:

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

For grouping:

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


Something like this?

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜