Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# 使用LinQ求和属性值_C#_Linq - Fatal编程技术网

C# 使用LinQ求和属性值

C# 使用LinQ求和属性值,c#,linq,C#,Linq,我有一个名为Product:List的自定义对象列表 class Product { string Key1 {get; set;} string Key2 {get; set;} int Count1 {get; set;} int Count2 {get; set;} } 我正在合并多个产品列表,我需要创建一个新列表,该列表将包含每个计数属性的总和值。 例如 因此,我的新清单应该是: New List: "Key1", "Key2", 6, 8 "Key2"

我有一个名为Product:List的自定义对象列表

class Product
{
    string Key1 {get; set;}
    string Key2 {get; set;}
    int Count1 {get; set;}
    int Count2 {get; set;}
}
我正在合并多个产品列表,我需要创建一个新列表,该列表将包含每个计数属性的总和值。 例如

因此,我的新清单应该是:

New List:
"Key1", "Key2", 6, 8
"Key2", "Key3", 10, 12
有人能帮我吗

谢谢。

你可以这样做

var list1 = new List<Product>()
   {
      new Product(){Key1 = "Key1", Key2 ="Key2", Count1 = 1, Count2 = 2},
      new Product(){Key1 = "Key2", Key2 ="Key3", Count1 = 1, Count2 = 2}
   };

var list2 = new List<Product>()
   {
      new Product(){Key1 = "Key1", Key2 ="Key2", Count1 = 6, Count2 = 8},
      new Product(){Key1 = "Key2", Key2 ="Key3", Count1 = 10, Count2 = 12}
   };

var result = list1.Concat(list2)
                  .GroupBy(x => new {x.Key1,x.Key2})
                  .Select(x => new
                     {
                        x.Key.Key1,
                        x.Key.Key2,
                        SumCount1 = x.Sum(y => y.Count1),
                        SumCount2 = x.Sum(y => y.Count2)
                     }).ToList();

其他资源

将指定集合的元素添加到 名单

根据指定的键对序列的元素进行分组 选择器函数,并使用 指定的函数

连接两个序列

你可以这样做

var list1 = new List<Product>()
   {
      new Product(){Key1 = "Key1", Key2 ="Key2", Count1 = 1, Count2 = 2},
      new Product(){Key1 = "Key2", Key2 ="Key3", Count1 = 1, Count2 = 2}
   };

var list2 = new List<Product>()
   {
      new Product(){Key1 = "Key1", Key2 ="Key2", Count1 = 6, Count2 = 8},
      new Product(){Key1 = "Key2", Key2 ="Key3", Count1 = 10, Count2 = 12}
   };

var result = list1.Concat(list2)
                  .GroupBy(x => new {x.Key1,x.Key2})
                  .Select(x => new
                     {
                        x.Key.Key1,
                        x.Key.Key2,
                        SumCount1 = x.Sum(y => y.Count1),
                        SumCount2 = x.Sum(y => y.Count2)
                     }).ToList();

其他资源

将指定集合的元素添加到 名单

根据指定的键对序列的元素进行分组 选择器函数,并使用 指定的函数

连接两个序列

List lst1=新列表();
List lst2=新列表();
lst1.Add(新产品(){Key1=“K1”,Key2=“K2”,Count1=1,Count2=2});
lst1.Add(新产品(){Key1=“K2”,Key2=“K3”,Count1=3,Count2=4});
添加(新产品(){Key1=“K1”,Key2=“K2”,Count1=5,Count2=6});
lst2.添加(新产品(){Key1=“K2”,Key2=“K3”,Count1=7,Count2=8});
//方式1
var l=lst1.Join(lst2,l1=>l1.Key1,l2=>l2.Key1,
(lt1,lt2)=>新产品{Key1=lt1.Key1,Key2=lt1.Key2,Count1=lt1.Count1+lt2.Count1,Count2=lt1.Count2+lt2.Count2});
//方式2
var result=lst1.Join(lst2,x=>new{x.Key1,x.Key2},
y=>new{y.Key1,y.Key2},(x,y)=>
新产品{Key1=x.Key1,Key2=x.Key2,Count1=x.Count1+y.Count1,Count2=x.Count2+y.Count2});
List lst1=新列表();
List lst2=新列表();
lst1.Add(新产品(){Key1=“K1”,Key2=“K2”,Count1=1,Count2=2});
lst1.Add(新产品(){Key1=“K2”,Key2=“K3”,Count1=3,Count2=4});
添加(新产品(){Key1=“K1”,Key2=“K2”,Count1=5,Count2=6});
lst2.添加(新产品(){Key1=“K2”,Key2=“K3”,Count1=7,Count2=8});
//方式1
var l=lst1.Join(lst2,l1=>l1.Key1,l2=>l2.Key1,
(lt1,lt2)=>新产品{Key1=lt1.Key1,Key2=lt1.Key2,Count1=lt1.Count1+lt2.Count1,Count2=lt1.Count2+lt2.Count2});
//方式2
var result=lst1.Join(lst2,x=>new{x.Key1,x.Key2},
y=>new{y.Key1,y.Key2},(x,y)=>
新产品{Key1=x.Key1,Key2=x.Key2,Count1=x.Count1+y.Count1,Count2=x.Count2+y.Count2});

您尝试过什么?您能解释一下列表1:K1、K2、1、2K2、K3、3、4的意思吗?它们是变量还是文字?你试过什么?你能解释一下你所说的
列表1:K1,K2,1,2k2,K3,3,4是什么意思吗?它们是变量还是文字?
Key1, Key2, 7, 10
Key2, Key3, 11, 14
 List<Product> lst1 = new List<Product>();
 List<Product> lst2 = new List<Product>();

 lst1.Add(new Product() {Key1 = "K1",Key2 ="K2", Count1 =1, Count2=2 });
 lst1.Add(new Product() { Key1 = "K2", Key2 = "K3", Count1 = 3, Count2 = 4 });

 lst2.Add(new Product() { Key1 = "K1", Key2 = "K2", Count1 = 5, Count2 = 6});
 lst2.Add(new Product() { Key1 = "K2", Key2 = "K3", Count1 = 7, Count2 = 8 });
// Way 1
 var l = lst1.Join(lst2, l1 => l1.Key1, l2 => l2.Key1, 
                (lt1, lt2) => new Product { Key1 = lt1.Key1, Key2 = lt1.Key2, Count1 = lt1.Count1 + lt2.Count1, Count2 = lt1.Count2 + lt2.Count2 } ).ToList() ;

// Way 2
var result = lst1.Join(lst2, x => new { x.Key1, x.Key2 },
                 y => new { y.Key1, y.Key2 }, (x, y) => 
                 new Product { Key1 = x.Key1, Key2 = x.Key2, Count1 = x.Count1 + y.Count1, Count2 = x.Count2 + y.Count2 }).ToList();