The GroupBy
groups all rows together that have the same key. In your case you're grouping by ProductId
therefore all entities that have the same ProductId
are grouped together. From this group you are then grabbing just the first product from that group so you're basically just getting the unique rows. If you want to group the products together and show the correct quantity then you need to count the items.
var report = printlist.GroupBy(x => x.ProductId) //Group items by product id
.Select(group => new Vi_Sales_Detail () { //For each group return back the product and the total # of items that match it
ProductId = group.Key, //The product that the group matches
Quantity = group.Count() //The # of items that are in group
})
.ToList(); //Make a list out of it
Of course your Vi_Sales_Detail
needs to have get/set properties that line up with the query I gave. Adjust accordingly.