Solved

LINQ Code Solution

Posted on 2013-11-09
9
394 Views
Last Modified: 2016-02-10
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
Comment
Question by:CSUGDEN
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
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

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

Expert Comment

by:Fernando Soto
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();

Open in new window

0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:CSUGDEN
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

by:Fernando Soto
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()

Open in new window

0
 

Author Comment

by:CSUGDEN
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

by:
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()

Open in new window

0
 

Author Closing Comment

by:CSUGDEN
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

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

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question