Extract Rows from DataTable using AsEnumerable and Linq Querry


As a database programmer you may love the for..loop, but as .Net programmer you can use many advanced features like Enumerables,Linq etc, which provide ease and peace.

Let’s begin with a DataSet, have many rows.My Products table contain a list of Medicine names. All I want to get the whole data in two line code with the Help of Linq query.

Language-Integrated Query (LINQ) is the name for a set of technologies based on the integration of query capabilities directly into the C# language Know More

Adapted from Doc.Microsft.com

Use the AsEnumerable()

AsENumerable method allows you to fetch rows as Enumerable string array, which actually not an array at all. We her try to fetch the rows an variant type variables with a Linq query.

Linq Query
var enum_names =from products in Product_dataset.Tables[0].AsEnumerable() select
products.Field<string>("name");

Print Result

 foreach (string str in enum_names){
Console.WriteLine(str);
}

1 thought on “Extract Rows from DataTable using AsEnumerable and Linq Querry

  1. Pingback: DataGridView meets Linq – A developer's Manual

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.