3

I am trying to get the distinct row with sum of each duplicate rows

DataTable newItems = new DataTable();

newItems.Columns.Add("Qty");
newItems.Columns.Add("BarcodeNumber");
newItems.Columns.Add("DisplayName");
newItems.Columns.Add("UnitPrice");
newItems.Columns.Add("TotalPrice");

newItems.Rows.Add(1, "URT29384", "Wallet", 88.8, 88.8);
newItems.Rows.Add(1, "URT29233", "Mouse", 4.44, 4.44);
newItems.Rows.Add(1, "URT29756", "Flash Drive", 1.47, 1.47);
newItems.Rows.Add(1, "URT29323", "Spoon", 99.95, 99.95);
newItems.Rows.Add(1, "URT29384", "Wallet", 88.8, 88.8);
newItems.Rows.Add(1, "URT29384", "Wallet", 88.8, 88.8);

i want the result to be

 3 URT29384  Wallet       266.4     266.4
 1 URT29233  Mouse          4.44      4.44
 1 URT29756  Flash Drive    1.47      1.47
 1 URT29323  Spoon         99.95     99.95

is this possible using LINQ? a little help here.. TYIA

Vincent Dagpin
  • 3,581
  • 13
  • 55
  • 85

3 Answers3

6

Yes, you can use Enumerable.GroupBy:

var result = newItems.AsEnumerable()
            .GroupBy(r => r.Field<String>("BarcodeNumber"))
            .Select(g => new { 
                Qty = g.Count(),
                BarcodeNumber = g.Key,
                DisplayName = g.First().Field<String>("DisplayName"),
                UnitPrice = g.Sum(r => r.Field<double>("UnitPrice")),
                TotalPrice = g.Sum(r => r.Field<double>("TotalPrice")),
            })
            .OrderByDescending(x => x.Qty);

Note that this is not a DataTable or IEnumerable<DataRow> but an anonymous type with your columns. You would need to use a foreach loop to add real DataRows to another DataTable if it's required.

foreach (var grp in result)
    Console.WriteLine("Qty:{0} BarcodeNumber:{1} DisplayName:{2} UnitPrice:{3} TotalPrice:{4}"
        , grp.Qty, grp.BarcodeNumber, grp.DisplayName, grp.UnitPrice, grp.TotalPrice);

You need to add using System.Linq;.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

Yes it is possible

var foo = newItems.AsEnumerable()
    .GroupBy(item => item.Field<string>(newItems.Columns["BarcodeNumber"]));

DataTable newTable = new DataTable();

newTable.Columns.Add("Qty");
newTable.Columns.Add("BarcodeNumber");
newTable.Columns.Add("DisplayName");
newTable.Columns.Add("UnitPrice");
newTable.Columns.Add("TotalPrice");

foreach (var item in foo)
{
    newTable.Rows.Add(
    item.Count(),
    item.Key,
    item.First().Field<string>(newItems.Columns["DisplayName"]),
    item.Sum(row => decimal.Parse(row.Field<string>(newItems.Columns["UnitPrice"]))),
    item.Sum(row => decimal.Parse(row.Field<string>(newItems.Columns["TotalPrice"]))));
}
Jan P.
  • 3,261
  • 19
  • 26
1
private static DataTable GetUniqueEntries(DataTable dt)  
    {  
        var query = (  
        from row in dt.AsEnumerable()  
        select row.Field<string>("UserName")).Distinct();  //any column <UserName>

        DataTable dtDistinctNames = new DataTable();  
        dtDistinctNames.Columns.Add("UserName", typeof(string));  

        //have to return a datatable, thus loop through entries  
        foreach (string item in query)  
        {  
            DataRow newRow = dtDistinctNames.NewRow();  
            newRow["UserName"] = item;  
            dtDistinctNames.Rows.Add(newRow);  
        }  

        return dtDistinctNames;  
    }
DareDevil
  • 5,249
  • 6
  • 50
  • 88