C# 比较LINQ中的两个类-获取';不匹配';
我有以下课程:C# 比较LINQ中的两个类-获取';不匹配';,c#,linq,C#,Linq,我有以下课程: public class DocumentCompare { public string Customer; public string Filename; public string Reference; public DateTime? Date; public override bool Equals(object obj) { if (obj == null) return fals
public class DocumentCompare
{
public string Customer;
public string Filename;
public string Reference;
public DateTime? Date;
public override bool Equals(object obj)
{
if (obj == null)
return false;
DocumentCompare doc = obj as DocumentCompare;
if ((Object)doc == null)
return false;
return (doc.Customer == Customer) && (doc.Date == Date) && (doc.Filename == Filename) && (doc.Reference == Reference);
}
public bool Equals(DocumentCompare doc)
{
if ((object)doc == null)
return false;
return (doc.Customer == Customer) && (doc.Date == Date) && (doc.Filename == Filename) && (doc.Reference == Reference);
}
public override int GetHashCode()
{
return string.Format("{0}_{1}_{2}_{3}",Customer,Filename,Reference,(Date == null ? "" : Date.Value.ToString())).GetHashCode();
}
}
我将检索这个类的两个列表-我想做的是比较这两个列表,并得到两个列表中都不存在的列表。所以,如果某个项目存在于x列表中,但不存在于y列表中,我想对该列表中的项目执行一个操作。如果某个项目存在于y列表中,但不存在于x列表中,则我希望执行不同的操作
我该怎么做?我想是用LINQ吧
编辑:性能不是什么大问题-这只会运行一次听起来你只是想: 旁白:
- 这不是一个非常好的生成哈希代码的方法——在这里搜索其他问题
- 从
委派到Equals(object)
以避免重复逻辑Equals(DocumentCompare)
- 可变类型不是进行相等比较的最佳候选类型(特别是在字典中将值用作键的类型,如果更改相等敏感组件,则将无法再次找到键)
- 即使您确实希望它是可变的,属性也比公共字段更适合封装
- 我要么密封该类型,要么检查这两个对象是否完全是同一类型,否则可能会导致不对称相等
- 这不是一个非常好的生成哈希代码的方法——在这里搜索其他问题
- 从
委派到Equals(object)
以避免重复逻辑Equals(DocumentCompare)
- 可变类型不是进行相等比较的最佳候选类型(特别是在字典中将值用作键的类型,如果更改相等敏感组件,则将无法再次找到键)
- 即使您确实希望它是可变的,属性也比公共字段更适合封装
- 我要么密封该类型,要么检查这两个对象是否完全是同一类型,否则可能会导致不对称相等
- 以下是代码:
var elementsMissingFromFirstList = firstList.Except(secondList).ToList();
var elementsMissingInSecondList = secondList.Except(firstList).ToList();
现在,您可以对这些缺失的元素执行操作:)以下是代码:
var elementsMissingFromFirstList = firstList.Except(secondList).ToList();
var elementsMissingInSecondList = secondList.Except(firstList).ToList();
现在您可以对这些缺少的元素执行操作:)您可以使用此方法比较两个不同列表中的对象。exmp:List和List x和y=DocumentCompare
public static bool EqualsObject<T>(this T t1, T t2) where T : class
{
var p1 = t1.GetType().Fields();
var p2 = t2.GetType().Fields();
for (int j = 0; j < p1.Length; j++)
{
var x = p1[j].GetValue(t1, null);
var y = p2[j].GetValue(t2, null);
if (x == null && y == null)
continue;
if (x != null && y == null)
return false;
if (x == null)
return false;
if (!x.Equals(y))
{
return false;
}
}
return true;
}
publicstaticboolequalobject(这是t1,t2),其中T:class
{
var p1=t1.GetType().Fields();
var p2=t2.GetType().Fields();
对于(int j=0;j
此方法将显示这两个列表之间的差异
public static List<T> DifferentObjects<T>(List<T> t, List<T> t2) where T : class
{
var diff = new List<T>();
if (t != null && t2 != null)
{
foreach (T t1 in t)
{
var state = false;
foreach (T t3 in t2.Where(t3 => EqualsObject(t1,t3)))
{
state = true;
}
if (!state)
{
diff.Add(t1);
}
}
}
return diff;
}
公共静态列表不同对象(列表t,列表t2),其中t:class
{
var diff=新列表();
如果(t!=null&&t2!=null)
{
foreach(T t1 in T)
{
var状态=假;
foreach(t2中的t3,其中(t3=>EqualsObject(t1,t3)))
{
状态=真;
}
如果(!状态)
{
差加(t1);
}
}
}
返回差;
}
您可以这样使用代码
var t = new List<DocumentCompare>();
var t2 = new List<DocumentCompare>();
t.Add(new DocumentCompare{Customer = "x"});
t.Add(new DocumentCompare{Customer = "y"});
t.Add(new DocumentCompare{Customer = "z"});
t2.Add(new DocumentCompare { Customer = "t" });
t2.Add(new DocumentCompare { Customer = "y" });
t2.Add(new DocumentCompare { Customer = "z" });
var list = DifferentObjects(t, t2);
var list2 = DifferentObjects(t2, t);
var t=newlist();
var t2=新列表();
t、 添加(新文档比较{Customer=“x”});
t、 添加(新文档比较{Customer=“y”});
t、 添加(新文档比较{Customer=“z”});
t2.添加(新文档比较{Customer=“t”});
t2.添加(新文档比较{Customer=“y”});
t2.添加(新文档比较{Customer=“z”});
var列表=不同的对象(t,t2);
var list2=不同的对象(t2,t);
您在类中使用了字段(客户、文件名等),因此GetType().fields();在EqualsObject方法中使用。如果使用属性,则应使用GetType().Properties();在EqualObject方法中。您可以使用此方法比较两个不同列表中的对象。exmp:List和List x和y=DocumentCompare
public static bool EqualsObject<T>(this T t1, T t2) where T : class
{
var p1 = t1.GetType().Fields();
var p2 = t2.GetType().Fields();
for (int j = 0; j < p1.Length; j++)
{
var x = p1[j].GetValue(t1, null);
var y = p2[j].GetValue(t2, null);
if (x == null && y == null)
continue;
if (x != null && y == null)
return false;
if (x == null)
return false;
if (!x.Equals(y))
{
return false;
}
}
return true;
}
publicstaticboolequalobject(这是t1,t2),其中T:class
{
var p1=t1.GetType().Fields();
var p2=t2.GetType().Fields();
对于(int j=0;j
此方法将显示这两个列表之间的差异
public static List<T> DifferentObjects<T>(List<T> t, List<T> t2) where T : class
{
var diff = new List<T>();
if (t != null && t2 != null)
{
foreach (T t1 in t)
{
var state = false;
foreach (T t3 in t2.Where(t3 => EqualsObject(t1,t3)))
{
state = true;
}
if (!state)
{
diff.Add(t1);
}
}
}
return diff;
}
公共静态列表不同对象(列表t,列表t2),其中t:class
{
var diff=新列表();
如果(t!=null&&t2!=null)
{
foreach(T t1 in T)
{
var状态=假;
foreach(t2中的t3,其中(t3=>EqualsObject(t1,t3)))
{
状态=真;
}
如果(!状态)
{
差加(t1);
}
}
}
返回差;
}
您可以这样使用代码
var t = new List<DocumentCompare>();
var t2 = new List<DocumentCompare>();
t.Add(new DocumentCompare{Customer = "x"});
t.Add(new DocumentCompare{Customer = "y"});
t.Add(new DocumentCompare{Customer = "z"});
t2.Add(new DocumentCompare { Customer = "t" });
t2.Add(new DocumentCompare { Customer = "y" });
t2.Add(new DocumentCompare { Customer = "z" });
var list = DifferentObjects(t, t2);
var list2 = DifferentObjects(t2, t);
var t=newlist();
var t2=新列表();
t、 添加(新文档比较)