Solved

query datable

Posted on 2013-11-11
10
211 Views
Last Modified: 2013-11-12
Is there a way to query datatable in vb.net?
sample I have datatable contains 500 row. column one is called "client_req_id".  I want to do like select distinct(client_req_id) from my datatable.
0
Comment
Question by:VBdotnet2005
  • 5
  • 4
10 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39640616
Hi VBdotnet2005;

The Linq query language can be used to query a DataTable object. If you give more information on the columns and what you wish to filter the table on and what you want the result set to look like we may be able to come up with some sample code.
0
 

Author Comment

by:VBdotnet2005
ID: 39640806
FernandoSoto,

sample datatable - I just want to find out a list for distinct(client_req_id ).

client_req_id     zipcode
78001                90001
78002                90002
78001                90003
78005                90002
etc

result would be

78001
78002
78005
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39641269
The ToTable method of the DataView object let you do it: http://msdn.microsoft.com/en-us/library/wec2b2e6(v=vs.110).aspx

I posted another link yesterday containing an example but apparently it was removed!
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39642621
Hi VBdotnet2005;

The following code snippet will iterate through your DataTable objects and return a List(Of Integer) with distinct values only.

Dim results = (From r In dt.AsEnumerable() _
               Order By r.Field(Of Integer)("client_req_id") _
               Select r.Field(Of Integer)("client_req_id")).Distinct().ToList()
               
' Where dt is the named instance of a DataTable object  

Open in new window

0
 

Author Comment

by:VBdotnet2005
ID: 39642718
Okay. That works for me. Since this is LINQ, I hope you don't mind,  how can I query selected fields (field1, field2, field3). I don't need it to return every column.


Dim results = (From r In dt.AsEnumerable() _
               where (f.field(of string)("client_req_id") = "BOFA_101"
             select filed1, field2, field3      <<<<<<<<

I want to copy Results to a different datatable.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39642802
Hi VBdotnet2005;

In the select start each field with a VB variable name the = then the Field to return followed by a comma. Continue this until you have identified all columns to be returned as shown in the code snippet below.

Dim results = (From r In dt.AsEnumerable() _
               Where r.Field(of String)("client_req_id") = "BOFA_101" _
               Select _
                   client_req_id = r.Field(of String)("client_req_id"), _
                   zipcode = r.Field(of String)("zipcode"), _
                   ' VariableName = r.Field(Of DataType)("String Column Name") _
                   ' All remaining fields here as above _
              )

Open in new window

0
 

Author Comment

by:VBdotnet2005
ID: 39642915
Can I convert results  to datatable?
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39642954
There is no function to convert the results into a DataTable object. If the DataTable already exists then it is just a matter of getting a blank row from that DataTable object using the DataTableInstance.NewRow() method and assigning the values the that row and then adding that NewRow back to the DataTable by using DataTableInstance.Rows.Add(TheNewRow). If the DataTable does not exist Create a DataTable and create a new DataColumn object for each column and adding those column to the DataTable object using DataTableInstance.Columns.Add(NewColumn). Once you have that you can follow the instructions above for when you have an existing table.
0
 

Author Comment

by:VBdotnet2005
ID: 39643205
Please see this link as a sample. I am not sure why I can't just select the column I need from a table and return it as a table.

http://matijabozicevic.com/blog/csharp-net-development/csharp-using-linq-to-query-datatable

var movies = from p in dtMovies.AsEnumerable()
             where p.Field<int>("Year") >= 1995
             select new
             {
                 ID = p.Field<int>("ID"),
                 Title = p.Field<string>("Title"),
                 Year = p.Field<int>("Year")
             };
 
 
// Writing some output...
foreach (var movie in movies)
{
    Console.WriteLine(string.Format("{0}. {1}  ({2})", movie.ID, movie.Title, movie.Year));
}


---


Dim results = From r In dt.AsEnumerable() _
               Where r.Field(of String)("client_req_id") = "BOFA_101" _
               Select  new {
                   client_req_id = r.Field(of String)("client_req_id"), _
                   zipcode = r.Field(of String)("zipcode")
                  }
0
 

Author Closing Comment

by:VBdotnet2005
ID: 39643432
i got it. Thank you
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SLMGR Switches Are Not Working On KMS Host 3 97
Help with LINQ and XML 10 36
Crystal reports vb.net 2 44
VB.net capture  result of a SQL query in variables 3 12
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

825 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