开发者

Convert piece of code into LINQ (short syntax)

开发者 https://www.devze.com 2023-01-18 05:59 出处:网络
I want to convert this开发者_运维问答 LINQ code var x = from nm in names select MyClass.SomeMethod(nm).TrimStart(\',\');

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

精彩评论

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