这是我试图开始工作的一些代码。如果一组中的项目与另一组中的项目不匹配,则将 0 添加到所有比较项目的列表中。如果最终列表不包含除 0 以外的任何其他值,则表示第一个集合中的项目在第二个集合中根本不存在。由于某种原因,我在结果列表中不断得到错误的值,所以某处一定有一个错误,只是我似乎找不到它。
public class CompareItem : IComparable
{
public string CustId { get; set; }
public string TechId { get; set; }
public CompareItem(string custId, string techId)
{
CustId = custId;
TechId = techId;
}
public int CompareTo(object obj)
{
CompareItem Temp = (CompareItem)obj;
if (this.CustId != Temp.CustId || this.TechId != Temp.TechId)
{
return 0;
}
else
{
return 1;
}
}
}
static void Main(string[] args)
{
List<CompareItem> LeftCompareSet = new List<CompareItem>();
LeftCompareSet1.Add(new CompareItem("0000", "0001"));
LeftCompareSet1.Add(new CompareItem("0001", "0001"));
LeftCompareSet1.Add(new CompareItem("0002", "0002"));
LeftCompareSet1.Add(new CompareItem("0003", "0003"));
LeftCompareSet1.Add(new CompareItem("0002", "0004"));
List<CompareItem> RightCompareSet = new List<CompareItem>();
RightCompareSet1.Add(new CompareItem("0005", "0005"));
RightCompareSet1.Add(new CompareItem("0004", "0004"));
RightCompareSet1.Add(new CompareItem("0003", "0003"));
RightCompareSet1.Add(new CompareItem("0002", "0002"));
RightCompareSet1.Add(new CompareItem("0006", "0002"));
int state = 0;
List<int> tlc = new List<int>();
List<int> trc = new List<int>();
foreach (CompareItem lc in LeftCompareSet)
{
foreach (CompareItem rc in RightCompareSet)
{
state = lc.CompareTo(rc);
if (state == 0)
{
tlc.Add(0);
}
else
{
tlc.Add(1);
}
}
if (tlc.Contains(1))
{
Console.WriteLine("Cust: " + lc.CustId + ", Tech: " + lc.TechId + ", Not missing");
}
else
{
Console.WriteLine("Cust: " + lc.CustId + ", Tech: " + lc.TechId + ", Missing");
}
}
foreach (CompareItem rc in RightCompareSet)
{
foreach (CompareItem lc in LeftCompareSet)
{
state = rc.CompareTo(lc);
if (state == 0)
{
trc.Add(0);
}
else
{
trc.Add(1);
}
}
if (trc.Contains(1))
{
Console.WriteLine("Cust: " + rc.CustId + ", Tech: " + rc.TechId + ", Not missing");
}
else
{
Console.WriteLine("Cust: " + rc.CustId + ", Tech: " + rc.TechId + ", Missing");
}
}
}