Solved

LINQ Code Solution

Posted on 2013-11-09
9
380 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
  • 5
  • 4
9 Comments
 
LVL 62

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 62

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
 

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 62

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 62

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 62

Expert Comment

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Icons and Colors for Terms 3 24
Help with Query not working in client's PC 1 35
DataGridView Events ? 3 37
How can my static class become undefined?? 8 0
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now