VBdotnet2005
asked on
query datable
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.
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.
ASKER
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
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
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!
I posted another link yesterday containing an example but apparently it was removed!
Hi VBdotnet2005;
The following code snippet will iterate through your DataTable objects and return a List(Of Integer) with distinct values only.
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
ASKER
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.
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.
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.
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 _
)
ASKER
Can I convert results to datatable?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.F ormat("{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")
}
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.F
}
---
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")
}
ASKER
i got it. Thank you
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.