【发布时间】:2013-04-04 22:12:55
【问题描述】:
如果 ID 匹配而文本不匹配,我如何匹配列表中的 2 个对象?
我的对象被添加到列表中:
List<MyObject> list = New List<MyObject>();
这可能是我的清单(这是一个对象):
ID Text
1 this is some text
2 text1
1 more text
1 a little more
2 text 2
3 XXX
那么我希望结果是:
ID Text
1 this is some text more text a little more
2 text1 text2
3 XXX
我已经尝试在 for 循环中使用 for ,但我只能弄清楚..
for (int i = 0; i < OrderList.Count; i++)
{
bool existsMoreThanOnce = false;
for (int j = i; j < OrderList.Count; j++)
{
duplicates.Add(OrderList[i]);
if (OrderList[i].OrderNumber == OrderList[j].OrderNumber && OrderList[i].OrderText != OrderList[j].OrderText)
{
if(!uniques.Contains(OrderList[j]))
{
duplicates.Add(OrderList[j]);
existsMoreThanOnce = true;
}
}
}
if (existsMoreThanOnce == false)
{
uniques.Add(OrderList[i]);
}
}
【问题讨论】:
-
你现在用什么代码来做这件事?
-
这个问题被否决了吗?
-
嘿 Mathias MyObject 中的属性是什么? ID(整数)和文本(字符串)?
-
hmm 我其实认为 id 是一个字符串.. 并且文本也是一个字符串,如果需要我可以将 id 更改为 int。