简体   繁体   中英

Entity Framework get count of distinct items

I have a table in my database that looks like this:

ID      value     channel
_________________________
1         50         201
2         55         201
3         60         201
4         10         202
5         12         202

And I want to get a count for the number of distinct channel values. So, in this case that count would be 2.

I am using visual C# with the Entity Framework.

Sorry if this is a simple question, but I couldn't find anything after googling.

Thanks for any help.

您可以使用Distinct()然后使用Count() ,如下所示:

int count = data.Select(d => d.channel).Distinct().Count();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM