如果ID匹配,C#合并字段值

时间:2013-04-12 11:40:17

标签: c# .net list merge

如果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 个答案:

答案 0 :(得分: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对象包含所需的输出。

答案 1 :(得分: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()) });

答案 2 :(得分:0)

var result = list1.Concat(list2)
                .GroupBy(x => x.ID)
                .Where(g => g.GroupBy(x=>x.Text).Count() > 1)
                .Select(x => x.Key)
                .ToList();

答案 3 :(得分: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将具有您想要的输出。