开发者

LINQ Select Distinct Count in Lambda form

开发者 https://www.devze.com 2023-03-17 18:33 出处:网络
Given a linq expression of an object collection \'items\' such as this: var total = (from item in items sel开发者_开发技巧ect item.Value).Distinct().Count()

Given a linq expression of an object collection 'items' such as this:

var total = (from item in items sel开发者_开发技巧ect item.Value).Distinct().Count()

Is it possible to convert this to use linq functions/lambdas:

items.Select(???).Distinct().Count()


Use this:

items.Select(i => i.Value).Distinct().Count()


It must be possible, since behind the scenes, LINQ is translated to lambdas and expression trees (at least LINQ to objects)

In your case the ??? part would be item => item.Value, i.e. for item, output item.value. So, the whole expression will be

var total = items.Select(item => item.Value).Distinct().Count();
0

精彩评论

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