Solved

LINQ Code Solution

Posted on 2013-11-09
9
377 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
Comment Utility
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
Comment Utility
The data should be grouped by ship_to.
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Not a problem, glad to help.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

6 Experts available now in Live!

Get 1:1 Help Now