如何在类列表中找到Duplicate并抛出异常 [英] How to find Duplicate in list of class and throw exception

查看:65
本文介绍了如何在类列表中找到Duplicate并抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,其中包含ID,Bankmisid

所以数据列表包含的内容就像是





Bankmisid | ID

1 | 45

2 | 45

3 | 45

4 | 46







所以我需要找到重复并抛出异常

I have a list in which it contain ID,Bankmisid
so the data list contain is like


Bankmisid | ID
1 | 45
2 | 45
3 | 45
4 | 46



so i need to find duplicate and throw exception

推荐答案

尝试:

Try:
if (myList.GroupBy(i => i.ID).Where(g => g.Count > 1).Select(g => g.Key).Count() > 0)
   {
   throw ...



[edit]哎呀![/ edit]


[edit]Oops![/edit]


这篇关于如何在类列表中找到Duplicate并抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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