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()));
精彩评论