比较List< string>和List< T> [英] compare List<string> and List<T>
问题描述
我正在使用C#和Framework 4.0.
I'm using C# and framework 4.0.
我有一个字符串类型的列表和另一个T类类型的列表;
I have a list of type string and another list of type class T;
如何比较列表和列表并保存差异?
How can I compare List with a List and save the difference?
private void simpleButton_Compare_Click(object sender, EventArgs e)
{
try
{
bool Is_Egal = true;
int i = 0;
foreach (string Od_Scan in Ordre_Scan)
{
if (!Outils.Get_Ordre_Donne()[i].NoOrdre.Contains(Od_Scan) && !String.IsNullOrWhiteSpace(Od_Scan))
{
Is_Egal = false;
Temp_Od_Scan.Add(Od_Scan);
}
i++;
}
foreach (Pers_Compare Od_Done in Outils.Get_Ordre_Donne())
{
if (!Ordre_Scan.Contains(Od_Done.NoOrdre) && !String.IsNullOrWhiteSpace(Od_Done.NoOrdre))
{
Is_Egal = false;
Temp_Od_Donne.Add(Od_Done);
}
else
{
Temp_Od_Donne_Egal.Add(Od_Done);
}
}
if (Is_Egal)
{
MessageBox.Show("égalité");
}
else
{
MessageBox.Show("PAS égalité");
}
}
catch (Exception excThrown)
{
MessageBox.Show(excThrown.Message);
}
}
和数据:
List<string> Ordre_Scan= new List<string> { "azer","qsdf"};
Pers_Compare obj = new Pers_Compare();
obj.Nolv = 1;
obj.Noordre = "qsdf"
Pers_Compare obj2 = new Pers_Compare();
obj2.Nolv = 1;
obj2.Noordre = "wxcv"
List<Pers_Compare> Ordre_Donne = new List<Pers_Compare>();
Ordre_Donne.add(obj);
Ordre_Donne.add(obj2);
我想将数据保存在Ordre_Donne中,而不要保存在Od_Scan中,反之亦然.
And I want to save the data in Ordre_Donne but not in Od_Scan and vice versa.
foreach (string Od_Scan in Temp_Od_Scan)
{
all item that not found in List A
--> wxcv
}
foreach (var Od_Done in Temp_Od_Donne)
{
all item that not found in List B
--> azer
}
推荐答案
The answer given for a slightly different question (comparing a List with another List) seems to me to be a good solution for your issue, they address multiple issues to do with comparisons of lists.
但是,您应该更具体地满足您的要求,即差异"到底是什么,例如{1,1,2}和{1,2}相同吗?
However you should be more specific with your requirements i.e. what exactly is a 'difference', e.g. is {1,1,2} and {1,2} the same?
以下是获得最高票数的答案...(此处包括只是为了避免由于某种原因(根据鲍勃的建议)将其删除)
Here is the answer given the most votes... (included here just encase it gets removed for some reason (as per Bob' suggestion))
" 说明: 我需要检查它们是否具有相同的元素,而不管它们在列表中的位置如何.每个MyType对象可能在列表上出现多次.有内置的功能可以检查吗?如果我保证每个元素在列表中仅出现一次怎么办?
" DESCRIPTION: I need to check that they both have the same elements, regardless of their position within the list. Each MyType object may appear multiple times on a list. Is there a built-in function that checks this? What if I guarantee that each element appears only once in a list?
伙计们感谢您的回答,但是我忘了添加一些内容,两个列表中每个元素的出现次数应该相同.
Guys thanks for the answers but I forgot to add something, the number of occurrences of each element should be the same on both lists.
答案: 如果您希望它们真正相等(即相同的项目,并且每个项目的编号相同),我认为最简单的解决方案是在比较之前进行排序:
ANSWER: If you want them to be really equal (i.e. the same items and the same number of each item), I think that the simplest solution is to sort before comparing:
Enumerable.SequenceEqual(list1.OrderBy(t => t), list2.OrderBy(t => t))
这是一种性能更好(大约快十倍)的解决方案,只需要IEquatable
而不是IComparable
:
public static bool ScrambledEquals<T>(IEnumerable<T> list1, IEnumerable<T> list2) {
var cnt = new Dictionary<T, int>();
foreach (T s in list1) {
if (cnt.ContainsKey(s)) {
cnt[s]++;
} else {
cnt.Add(s, 1);
}
}
foreach (T s in list2) {
if (cnt.ContainsKey(s)) {
cnt[s]--;
} else {
return false;
}
}
return cnt.Values.All(c => c == 0);
}
要处理任何数据类型作为键(例如,Frank Tzanabetis指出的可为空的类型),您可以制作一个使用"
这篇关于比较List< string>和List< T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!