?
Solved

LINQ Code Solution

Posted on 2013-11-09
9
Medium Priority
?
399 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 64

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 64

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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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 64

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 64

Accepted Solution

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

Expert Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

719 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