【问题标题】:C# merge field values if the ID's match如果 ID 匹配,C# 合并字段值
【发布时间】: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。

标签: c# .net list merge


【解决方案1】:

您可以从 LINQ 的 GroupBy 开始。

var output = input.GroupBy(i => i.ID)
                  .Select(i => new { ID = i.Key, 
                                   Text = String.Join(" ", 
                                        i.Select(x => x.Text).ToArray()) });

【讨论】:

  • 如何使用 GroupBy?我不能说:MyList.Groupby
  • 顶部有using System.Linq吗?
  • @Blam 没有 System.Text.Linq 命名空间。程序集是System.Core
  • Daniel,我已经添加了它,现在我收到了这个错误:string.join(string, params object[]) 的最佳重载方法匹配有一些无效参数
  • 尝试将String.Join(i)更改为String.Join(" ", i.ToArray())
【解决方案2】:
var result = list1.Concat(list2)
                .GroupBy(x => x.ID)
                .Where(g => g.GroupBy(x=>x.Text).Count() > 1)
                .Select(x => x.Key)
                .ToList();

【讨论】:

  • 不鼓励仅使用代码的答案。你能添加一些说明为什么它会这样吗?
  • 好吧..你能把答案只基于一个列表吗?
  • @Mathias 只需删除 .Concat(list2)。现在你只有一个列表:list1
【解决方案3】:

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

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 将具有您想要的输出。

【讨论】:

    【解决方案4】:

    首先我创建一个类

    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 对象包含所需的输出。

    【讨论】:

      猜你喜欢
      • 2015-01-19
      • 1970-01-01
      • 2018-04-07
      • 2019-02-10
      • 2022-01-15
      • 1970-01-01
      • 2022-11-05
      • 2014-08-21
      • 2014-05-09
      相关资源
      最近更新 更多