LINQ Select Distinct Count in Lambda form

Kyle picture Kyle · Jul 7, 2011 · Viewed 33.4k times · Source

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

var total = (from item in items select item.Value).Distinct().Count()

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

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

Answer

Daniel Hilgarth picture Daniel Hilgarth · Jul 7, 2011

Use this:

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