Solved

LINQ Code Solution

Posted on 2013-11-09
9
396 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
AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

 

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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

624 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