-1

如果它们的 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]);
            }
        }
4

4 回答 4

1

首先我创建一个类

public class Items
{
    public int ID { get; set; }
    public string Text { get; set; }

    public Items(int id, string text)
    {
        ID = id;
        Text = text;
    }
}

现在我的代码逻辑是

List<Items> objItems = new List<Items>();
        objItems.Add(new Items(1,"Rahul"));
        objItems.Add(new Items(2, "Rohit"));
        objItems.Add(new Items(1, "Kumar"));
        objItems.Add(new Items(2, "Verma"));
        List<Items> objNew = new List<Items>(); //it will hold result
        string str = "";

        for (int i = 0; i < objItems.Count; i++)
        {
            if (objItems[i].ID > 0)
            {
                str = objItems[i].Text;
                for (int j = i + 1; j < objItems.Count; j++)
                {
                    if (objItems[i].ID == objItems[j].ID)
                    {
                        str += objItems[j].Text + " ";
                        objItems[j].ID = -1;
                    }
                }
                objNew.Add(new Items(objItems[i].ID, str));
            }
        }

ObjNew 对象包含所需的输出。

于 2013-04-12T14:17:46.967 回答
0

首先我创建了一个类来保存你的列表

public class MyObject
{
    public int ID { get; set; }
    public string Text { get; set; }
}

然后我将虚拟值插入其中

List<MyObject> obj = new List<MyObject>
{
     new MyObject{ID=1, Text="this is some text"},
     new MyObject{ID=2, Text="text1"},
     new MyObject{ID=1, Text="more text"},
     new MyObject{ID=1, Text="a little more"},
     new MyObject{ID=2, Text="text 2"},
     new MyObject{ID=3, Text="XXX"}
};

List<MyObject> obj2 = new List<MyObject>(); //this list will hold your output
//the linq query will filter out the uniques ids.
var uniqueIds = (from a in obj select new { a.ID, a.Text }).GroupBy(x => x.ID).ToList();
//then iterated through all the unique ids to merge the texts and list them under the unique ids.
int id=0;
foreach (var item in uniqueIds)
{
    string contText = "";
    for (int j = 0; j < item.Count(); j++)
    {
        contText += item.ElementAt(j).Text + " ";
        id = item.ElementAt(j).ID;
    }

    obj2.Add(new MyObject { ID = id, Text = contText });               
 }

列表 obj2 将具有您想要的输出。

于 2013-04-12T13:46:27.740 回答
0

您可以从 LINQ 的GroupBy.

var output = input.GroupBy(i => i.ID)
                  .Select(i => new { ID = i.Key, 
                                   Text = String.Join(" ", 
                                        i.Select(x => x.Text).ToArray()) });
于 2013-04-12T11:41:49.677 回答
0
var result = list1.Concat(list2)
                .GroupBy(x => x.ID)
                .Where(g => g.GroupBy(x=>x.Text).Count() > 1)
                .Select(x => x.Key)
                .ToList();
于 2013-04-12T11:48:04.527 回答