如何在C#中的列表中计算差异 [英] How to Count Distinct in a list in c#

查看:66
本文介绍了如何在C#中的列表中计算差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个字符串类型列表,并且已将字符串添加为

``abc'',``abc'',``xyz''.


我想计算此列表的不同值.

以上的输出应为2.

谢谢
mohd wasif

Hi,
I have a List of string type and I have added string as

''abc'',''abc'',''xyz''.


I want to count the distinct values of this list .

Output should be 2 for above.

thanks
mohd wasif

推荐答案


另一种方式
Hi ,
Another way
List<string> lst = new List<string> { "abc", "abc", "xyz" };
       int res = (from x in lst
                  select x).Distinct().Count();


最好的问候
M.Mitwalli


Best Regards
M.Mitwalli


可能是这样的:

It can be something like:

List<string> myList = new List<string> {"abc", "abc", "xyz"};
int distinctCount = myList.Distinct().Count();




检查这些.

101个LINQ示例 [以及人人平等...匿名类型" [ ^ ]
使用不同的代码并避免使用Lambdas [ ^ ]
Enumerable.Distinct< tsource>方法(IEnumerable< tsource>) [ ^ ]

希望对您有所帮助.
Hi,

check these.

101 LINQ Samples[^]
And Equality for All ... Anonymous Types[^]
Using Distinct and Avoiding Lambdas[^]
Enumerable.Distinct<tsource> Method (IEnumerable<tsource>)[^]

hope it helps.


这篇关于如何在C#中的列表中计算差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆