• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5273
  • Last Modified:

LINQ: group by from datatable

Hi,

I read a DBF file (with more than 1.000.000 records) into a datatable.
I can read that data, but now I want to group by on 2 columns (sampnumber,testcode)

Once I know the unique records by (sampnumber,testcode), I want the last 29 records for  every sampnumber,testcode in the data.

I've been looking for the solution, but I just can't find it.

The columns I have:
sampnumber, sampname, samplenr, date, testcode, testname, result, unit

Thanks.
0
VampValdo
Asked:
VampValdo
  • 3
  • 3
1 Solution
 
BuggyCoderCommented:
Here is sample code:-
var dt = new DataTable();
            dt.Columns.Add("Number", typeof (int));
            dt.Columns.Add("Code", typeof(int));
            dt.Columns.Add("Unit", typeof(int));

            var dr = dt.NewRow();
            dr.SetField<int>("Number", 1);
            dr.SetField<int>("Code", 12);
            dr.SetField<int>("Unit", 3);

            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr.SetField<int>("Number", 1);
            dr.SetField<int>("Code", 13);
            dr.SetField<int>("Unit", 34);

            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr.SetField<int>("Number", 1);
            dr.SetField<int>("Code", 12);
            dr.SetField<int>("Unit", 13);

            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr.SetField<int>("Number", 1);
            dr.SetField<int>("Code", 13);
            dr.SetField<int>("Unit", 7);

            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr.SetField<int>("Number", 1);
            dr.SetField<int>("Code", 14);
            dr.SetField<int>("Unit", 18);

            dt.Rows.Add(dr);

            var qry = from rw in dt.AsEnumerable()
                      group rw by new {Number = rw["Number"], Code = rw["Code"]}
                      into grouping
                      select
                          new
                              {
                                  grouping.Key.Number,
                                  grouping.Key.Code,
                                  Unit = grouping.Sum(rw => Convert.ToInt32(rw["Unit"]))
                              };

            foreach(var q in qry)
            {
                
            }

Open in new window

0
 
VampValdoAuthor Commented:
Hi, thank you.

I can't translate that into VB.NET code. :'(

Do I then have to loop in the foreach and do a new linq that gives me the top 29 records for every record where sampnumber and testcode are the same as the values from the first linq? With a where?
0
 
BuggyCoderCommented:
Here is sample LINQ using Method Chains(My Preference):-

var qry =
                dt.AsEnumerable()
                    .GroupBy(rw => new {Number = rw["Number"], Code = rw["Code"]})
                    .Select(grouping => new
                                            {
                                                grouping.Key.Number,
                                                grouping.Key.Code,
                                                Unit = grouping.Sum(rw => Convert.ToInt32(rw["Unit"]))
                                            });
                                            

            foreach(var q in qry)
            {
                
            }

Open in new window


In VB :-
Dim qry = dt.AsEnumerable().GroupBy(Function(rw) New With { _
	.Number = rw("Number"), _
	.Code = rw("Code") _
}).[Select](Function(grouping) New With { _
	grouping.Key.Number, _
	grouping.Key.Code, _
	.Unit = grouping.Sum(Function(rw) Convert.ToInt32(rw("Unit"))) _
})



For Each q As var In qry
Next

Open in new window


Used This For Conversion:-
http://converter.telerik.com/
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
VampValdoAuthor Commented:
Hi,

This code is not working.

I get the following error:
error
I even tried using sampnumber for the sum (that are all whole numbers)

But I just need a list of the unique values of (sampnumber / testcode).
0
 
BuggyCoderCommented:
this probably means that it cannot be converted to integer
Just see if its type is integer....

Otherwise use the same type as in your datatable...
0
 
käµfm³d 👽Commented:
See if this helps you:

Dim query = From row As DataRow In dt.Rows
            Group row By SampNumber = row.Field(Of Integer)("sampnumber"), TestCode = row.Field(Of Integer)("testcode") Into Group
            Select Group

For Each grp In query
  Dim last29 = grp.Skip(grp.Count() - 29)

  For Each item In last29
    ' do something with last 29 for each grp
  Next
Next

Open in new window

0
 
VampValdoAuthor Commented:
Worked like a charm.

Thank you. :-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now