Solved

LINQ Code Solution

Posted on 2013-11-09
382 Views
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
0
Question by:CSUGDEN
• 5
• 4

LVL 63

Expert Comment

ID: 39635908
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 Comment

ID: 39635959
The data should be grouped by ship_to.
0

LVL 63

Expert Comment

ID: 39635981
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 Comment

ID: 39636001
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

LVL 63

Expert Comment

ID: 39636011
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 Comment

ID: 39636022
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

LVL 63

Accepted Solution

Fernando Soto earned 300 total points
ID: 39636036
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

Author Closing Comment

ID: 39636044
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

LVL 63

Expert Comment

ID: 39636076
Not a problem, glad to help.
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldnâ€™t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldnâ€™t it be good if you could write code like this? (CODE) In .NET 3.5, this is possibleâ€¦
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching â€¦
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlinâ€¦