# LINQ Code Solution

Howdy,

I am trying to pick up some LINQ skills and I was stumped by this latest problem I am facing.
I have a System.Data.DataTable with soem data in it. Rwo columns primarily I need to use: CHK and SHIP_TO. I would like to create some LINQ query to return the number of groups within the table that have a value of 1 in the CHK column.

The answer may be in C# or VB.

Can anyone take a stab at it?

Regards
###### Who is Participating?

RetiredCommented:
Try it like this.

``````Dim results = (From t in DataTableObject.AsEnumerable() _
Where t.Field(Of Integer)("CHK") = 1 _
Group t By Key = t.Field(Of String)("SHIP_TO") Into Group _
Select TotalGroups = Group).Count()
``````
0

RetiredCommented:
When you say the number of groups what column do you want to group the results by that have 1 in CHK column.
0

Author Commented:
The data should be grouped by ship_to.
0

RetiredCommented:
Hi CSUGDEN;

The following query will group the datatable by SHIP_TO and who has a 1 for CHK and return an integer with the total number of groups.

``````var results = (from t in DataTableObject.AsEnumerable()
where t.Field<int>("CHK") == 1
group t by t.SHIP_TO into shipToGrouping
select shipToGrouping).Count();
``````
0

Author Commented:
I thought I could just convert it to VB for the project that I am on but I get an error
'Into Expected' on the converted code:
Dim results = (From shipToGrouping In From t In DataTableObject.AsEnumerable() Where t.Field(Of Integer)("CHK") = 1Group t By t.SHIP_TOshipToGrouping).Count()
0

RetiredCommented:
Try it like this.

``````Dim results = (From t in DataTableObject.AsEnumerable() _
Where t.Field(Of Integer)("CHK") = 1 _
Group t By t.Field(Of String)("SHIP_TO") Into Group _
Select Group).Count()
``````
0

Author Commented:
I get another error
Error      1      Range variable name can be inferred only from a simple or qualified name with no arguments.      D:\Projects\DataServer\Convertible.vb      79      27      DataServer
0

Author Commented:
That looks like the solution to me. The syntax on some of these queries really kills me.
Thanks. I'll study this for future use.
0

RetiredCommented:
Not a problem, glad to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.