搜索重复的条目在列表<&文本框GT;使用LINQ [英] Search duplicate entries in List<TextBox> using LINQ

查看:164
本文介绍了搜索重复的条目在列表<&文本框GT;使用LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含TextBox控件列表。基本上,这一切归一组框控件中。

I have a list containing TextBox controls. Basically all this grouped inside a groupbox control.

List<TextBox> listTextBox;



我需要只要调用组框验证事件,以检查这些文本框中的任何重复的条目。

I need to check for any duplicate entries within these TextBoxes whenever the groupbox validation event is invoked.

我只能想到枯燥的方式是遍历列表顺序,每次解析文本框的值,发现如果有多个条目具有相同的值

I can only think of the boring way is to loop through the list sequentially and parse the textbox value each time and find if there is more than one entry with the same value.

任何一个可以帮助我通过使用谓词来更高效地完成相同的操作。

Can any one help me by using a Predicate to do the same operation more efficiently.

有关EX

list.Count(//predicate that gets the value for all textbox);

如果计数大于1,那么我敢肯定有在文本框中重复的条目。

if the count is more than 1, Then I'm sure there are duplicate entries in the textbox.

有没有其他更好的办法?

Is there any other better way ?

干杯
VATSAG

Cheers VATSAG

推荐答案

您可以使用 GROUPBY ,然后用计数<筛选组/ code>> 1,选择所有此类文本。我没有遵守的代码,但这个应该是非常接近。

You could use GroupBy and then filter the groups with a Count > 1 and select the Key for all such texts. I have not complied the code but this should be close enough.

var duplicateTexts=
        listTextBox.GroupBy(tb => tb.Text)
            .Where(group => group.Count() > 1)
            .Select(group => group.Key);

这篇关于搜索重复的条目在列表&LT;&文本框GT;使用LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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