Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#合并两个列表,无重复项,并对字段值求和_C#_Generics - Fatal编程技术网

C#合并两个列表,无重复项,并对字段值求和

C#合并两个列表,无重复项,并对字段值求和,c#,generics,C#,Generics,我有一个类具有Id、Product、Quantity属性。我已经为这个类创建了一个通用的列表类型,有些项的值是相同的(如下所示)。因此,我想合并没有重复项的相同项目,但也要将数量字段值求和为一个项目,并创建一个没有重复项但数量字段值为总和的新通用列表。但无论如何,我都做不到 代码如下: class Program { static void Main(string[] args) { Product p1 = new Product(1, "apple", 1);

我有一个类具有Id、Product、Quantity属性。我已经为这个类创建了一个通用的列表类型,有些项的值是相同的(如下所示)。因此,我想合并没有重复项的相同项目,但也要将数量字段值求和为一个项目,并创建一个没有重复项但数量字段值为总和的新通用列表。但无论如何,我都做不到

代码如下:

class Program
{
    static void Main(string[] args)
    {
        Product p1 = new Product(1, "apple", 1);
        Product p2 = new Product(1, "apple", 1);
        Product p3 = new Product(1, "apple", 1);
        Product p4 = new Product(1, "apple", 1);
        Product p5 = new Product(2, "orange", 1);
        Product p6 = new Product(2, "orange", 1);
        Product p7 = new Product(3, "mango", 1);
        Product p8 = new Product(3, "mango", 1);
        Product p9 = new Product(3, "mango", 1);
        Product p10 = new Product(3, "mango", 1);

        List<Product> list = new List<Product>();
        list.Add(p1);
        list.Add(p2);
        list.Add(p3);
        list.Add(p4);
        list.Add(p5);
        list.Add(p6);
        list.Add(p7);
        list.Add(p8);
        list.Add(p9);
        list.Add(p10);

        var list_collapsed = Collapse(list);

        foreach (var item in list_collapsed)
        {
            Console.WriteLine("Id:{0} - Product: {1} - Quantity: {2}", item.Id, item.Name, item.Quantity);
        }

        Console.ReadKey();
    }

    public static List<Product> Collapse(List<Product> ExpandedList)
    {
        List<Product> CollapsedList = new List<Product>();

        for (int i = 0; i < ExpandedList.Count; i++)
        {
            if (CollapsedList.Count == 0)
            {
                CollapsedList.Add(ExpandedList[i]);
            }

            else
            {
                for (int j = 0; j < CollapsedList.Count; j++)
                {
                    if (ExpandedList[i].Id != CollapsedList[j].Id)
                    {
                        CollapsedList.Add(ExpandedList[i]);
                        break;
                    }

                    else
                    {
                        CollapsedList[j].Quantity += 1;
                        break;
                    }
                }
            }
        }

        return CollapsedList;
    }
}

class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Quantity { get; set; }

    public Product(int id, string name, int quantity)
    {
        this.Id = id;
        this.Name = name;
        this.Quantity = quantity;
    }
}
类程序
{
静态void Main(字符串[]参数)
{
产品p1=新产品(1,“苹果”,1);
产品p2=新产品(1,“苹果”,1);
产品p3=新产品(1,“苹果”,1);
产品p4=新产品(1,“苹果”,1);
产品p5=新产品(2,“橙色”,1);
产品p6=新产品(2,“橙色”,1);
产品p7=新产品(3,“芒果”,1);
产品p8=新产品(3,“芒果”,1);
产品p9=新产品(3,“芒果”,1);
产品p10=新产品(3,“芒果”,1);
列表=新列表();
列表。添加(p1);
列表。添加(p2);
列表。添加(p3);
列表。添加(p4);
增加(p5);
增加(第6页);
添加列表(第7页);
添加列表(第8页);
添加列表(第9页);
列表。添加(第10页);
var list_collapsed=折叠(列表);
foreach(列表中的变量项已折叠)
{
WriteLine(“Id:{0}-产品:{1}-数量:{2}”、item.Id、item.Name、item.Quantity);
}
Console.ReadKey();
}
公共静态列表折叠(列表展开列表)
{
List CollapsedList=新列表();
for(int i=0;i
输出:

Id:1-产品:苹果-数量:3
Id:2-产品:橙色-数量:1
Id:2-产品:橙色-数量:1
Id:3-产品:芒果-数量:1
Id:3-产品:芒果-数量:1
Id:3-产品:芒果-数量:1
Id:3-产品:芒果-数量:1

但我需要它的输出,如下所示:

Id:1-产品:苹果-数量:3
Id:2-产品:橙色-数量:2
Id:3-产品:芒果-数量:4


有人能帮我吗

是的,您可以使用Id字段进行分组。然后在每个组中,您可以汇总数量字段

 public static List<Product> Collapse(List<Product> ExpandedList)
 {
 List<Product> CollapsedList = new List<Product>();

 var groupBy = ExpandedList.GroupBy(x => x.Id);

 foreach (var group in groupBy)
 {
  var first = group.FirstOrDefault();
  first.Quantity = group.Sum(x => x.Quantity);
  CollapsedList.Add(first);
 }

 return CollapsedList;
 }
公共静态列表折叠(列表展开列表)
{
List CollapsedList=新列表();
var groupBy=ExpandedList.groupBy(x=>x.Id);
foreach(groupBy中的var组)
{
var first=group.FirstOrDefault();
第一个.Quantity=group.Sum(x=>x.Quantity);
CollapsedList.Add(第一个);
}
返回折叠列表;
}

是的,您可以使用Id字段进行分组。然后在每个组中,您可以汇总数量字段

 public static List<Product> Collapse(List<Product> ExpandedList)
 {
 List<Product> CollapsedList = new List<Product>();

 var groupBy = ExpandedList.GroupBy(x => x.Id);

 foreach (var group in groupBy)
 {
  var first = group.FirstOrDefault();
  first.Quantity = group.Sum(x => x.Quantity);
  CollapsedList.Add(first);
 }

 return CollapsedList;
 }
公共静态列表折叠(列表展开列表)
{
List CollapsedList=新列表();
var groupBy=ExpandedList.groupBy(x=>x.Id);
foreach(groupBy中的var组)
{
var first=group.FirstOrDefault();
第一个.Quantity=group.Sum(x=>x.Quantity);
CollapsedList.Add(第一个);
}
返回折叠列表;
}

是的,林克是你的朋友:

var list_collapsed = products.GroupBy(p => p.Name).Select(g => new Product(0, p.Key, p.Sum(x => x.Quantity)).ToList();

是的,林克是你的朋友:

var list_collapsed = products.GroupBy(p => p.Name).Select(g => new Product(0, p.Key, p.Sum(x => x.Quantity)).ToList();

您只需要一个小小的查询:

var list_collapsed = list
    .GroupBy(p => new { Id = p.Id, Name = p.Name } )
    .Select(g => new Product(g.Key.Id, g.Key.Name, g.Sum(p => p.Quantity)))
    .ToList();

您只需要一个小小的查询:

var list_collapsed = list
    .GroupBy(p => new { Id = p.Id, Name = p.Name } )
    .Select(g => new Product(g.Key.Id, g.Key.Name, g.Sum(p => p.Quantity)))
    .ToList();

var list\u collapsed=products.GroupBy(p=>p.id)。选择(g=>newproduct(id=g.Key.id,Name=g.Key.Name,Quantity=g.Sum(p=>p.Quantity)).ToList();
var list\u collapsed=products.GroupBy(p=>p.id)。选择(g=>newproduct(id=g.Key.id,Name=g.Key.Name,Quantity=g.Sum(p=>p.Quantity)).ToList)();

试试这个:

        var newList = from p in list
                      group p by p.Id
                      into g
                      select new Product(g.Key, g.Min(f => f.Name), g.Sum(p => p.Quantity));
试试这个:

        var newList = from p in list
                      group p by p.Id
                      into g
                      select new Product(g.Key, g.Min(f => f.Name), g.Sum(p => p.Quantity));