Tonight read a predecessor wrote LINQ to the collection elements merge to repeat processing, feel a bit of trouble, the original address is as follows: Http://www.cnblogs.com/yjmyzz/archive/2012/12/18/2823170.html#undefined.
So I figured I'd use the LINQ grouping function to write a method with the following code:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace ConsoleApplication4
{
Class Program
{
static void Main ()
{
list<product> listproduct = new List<product> {
New product{stocknum=1,productno= "tag=", "a"},
New product{stocknum=2,productno= "tag=", "B"},
New product{stocknum=3,productno= "tag=", "C"},
New product{stocknum=4,productno= "", tag= "D"},
New product{stocknum=5,productno= "tag=", "E"},
};
list<product> listProduct2 = new list<product> ();
var group = Listproduct.groupby (P=>p.productno);
foreach (igrouping<string,product> groupsequence in Group)
{
Product p2= New Product
{
Productno=groupsequence.first (). Productno,
Stocknum=groupsequence.sum (P=>p.stocknum),
Tag=string. Join (",", groupsequence.select (t = = T.tag). ToArray ())
};
Listproduct2.add (p2);
}
Output validation
Listproduct2.foreach (c =
{
Console.WriteLine ("Productno={0},stocknum={1},tag={2}", C.productno, C.stocknum, C.tag);
});
Console.read ();
}
}
<summary>
Product entity class
</summary>
Class Product
{
<summary>
Inventory
</summary>
public int Stocknum {set; get;}
<summary>
Product number
</summary>
Public String Productno {set; get;}
<summary>
Accessory Label
</summary>
Public String Tag {set; get;}
}
}
Merging and de-duplicating collection Elements with LINQ