score:3
Generally LINQ queries works on data sources which implement the IEnumerable<T>/ IQueryable<T> Interface
. But DataTable does not implement any of these. So we can not directly apply LINQ queries on a DataTable.
But DataTable class has an extension method called AsEnumerable
which returns an IEnumerable
collection of DataRow. So we can apply the AsEnumerable
function on a DataTable and then play with some LINQ on the resulting collection.
var items=(from p in myDataTable.AsEnumerable()
select new { ID= p.Field<int>("ID").
Name=p.Field<string>("Name")
}).ToList();
var filtered=items.Where(x => x.Name.Contains("Mike"));
EDIT : Here is the VB.NET Version ( Disclaimer: I am not a VB.NET guy. but i could build this code without any error)
Dim items = (From p In myDataTable.AsEnumerable()
Select New With {.ID = p.Field(Of Integer)("ID"),
.Name = p.Field(Of String)("Name")}).ToList()
Dim filtered = items.Where(Function(x) x.Name.Contains("Mike")).ToList()
score:0
VB
Private Function likes(ByVal dt As DataTable, ByVal column As String, ByVal value As String)
Dim result = dt.Clone()
For Each row As DataRow In From row1 As DataRow In dt.Rows Where (row1(column).Contains(value))
result.ImportRow(row)
Next
Return result
End Function
C#
private DataTable likes(ref DataTable dt, string column, string value)
{
DataTable result = dt.Clone();
foreach (DataRow row in from row1 in dt.Rowswhere (row1(column).Contains(value))) {
result.ImportRow(row);
}
return result;
}
Source: stackoverflow.com
Related Articles
- DataTable select method with single quote conflict C#
- C# DataTable select columns where column name like
- DataTable select with LiNQ and check is there duplicate rows or not
- generic method to get linq result to datatable for select new with multiple selects
- Select From DataTable with Multiple Conditions
- How do i use linq and select just a Sum of values(field) from a datatable along with a where clause?
- Code First Entity Framework, select ViewModel - constructor with parameter
- Select entire duplicate row from datatable with linq c#
- Select top N with record from DataTable with some sorting using Linq
- VB.NET LINQ To DataTable Select with Where Clause
- How to do a wildcard DataTable select with lambda/LINQ using question marks?
- Select all records of a datatable with duplicate values
- Can I have a select like this with a LINQ to Entitites query?
- How to run Linq on DataTable with SQL like 'IN' and return DataTable C#?
- Select and sum DataTable rows with criteria
- How to use LINQ to select object with minimum or maximum property value
- Select a Dictionary<T1, T2> with LINQ
- LINQ Select Distinct with Anonymous Types
- Select multiple records based on list of Id's with linq
- How to select only the records with the highest date in LINQ
- Linq code to select one item
- How can I do SELECT UNIQUE with LINQ?
- What does this C# code with an "arrow" mean and how is it called?
- Querying Datatable with where condition
- Linq : select value in a datatable column
- How to write linq query to match SQL like select top 100 * from tab?
- Using Linq with 2D array, Select not found
- lambda expression join multiple tables with select and where clause
- Select all columns on an object with Linq
- Select single column from dataset with LINQ
- Select Same Element on Second List By Order of Second List
- LINQ to SQL Associations Not Generating
- Filtering data according to date range mvc4
- List Sorting with specific text and order based on user choice
- Optimizing query with many-to-many relationship on big data set
- How to set where condition on parent and child table
- Using LINQ expression to retrieve IEnumerable of properties from IEnumerable of POCO
- Linq, how can I do flexible sorting?
- Convert one list to the other list type
- How to integrate external list to raw sql query join as table in a linq raw sql query
- LINQ to SQL debug visualizer for VS 2012?
- How to sort an IEnumerable<AnonymousType> or sort a two datatable join using LINQ?
- Count object with same attributevalues in list of objects with GroupBy
- get distinct item in List with highest value in property
- Need to update list item and then put it in textfile
- Calculate DateTime Difference Sum within Group Linq to Entity
- Get the Substring of a List in C#
- How do I cast IQueryable<System.Guid> to a string?
- LINQs Joins vs Stored Procedures Joins
- Simplest LINQ in C# troubleshooting