Here we learn how to write LINQ query with ado.net dataset, some real-time example of using LINQ in asp.net
Though LINQ was not designed to use with ADO.Net objects;
Because ADO.Net was based on different approach that we have used so far in application development.
But just consider if you have any application already developed using ADO.Net, now can you write LINQ queries on your existing ado.net code? The answer is Yes!
In previous tutorial we have already learned about ADO.Net Objects, Now you learn how to write LINQ queries with ADO.Net object like dataset, datatable etc.
Here is the code
var _students = from dt in _ds.Tables[0].AsEnumerable() where (dt.Field<string>("FullName").StartsWith("A")) select new { Name = dt.Field<string>("FullName"), Address = dt.Field<string>("Address") };
Now simply assign the collection object to a gridview
gvStudents.DataSource = _students; gvStudents.DataBind();
Now you have basic idea about how LINQ works, in LINQ you can query to any data source like SQL, XML, Entity Framework, ADO.Net and more, but Ado.Net works differently where you can execute query using any ado.net objects which can work with any RDBMS like Oracle, SQL etc.
In this section we will try to a comparative overview of Ado.Net and LINQ
Ado.Net | LINQ |
Ado.net was introduced with very initial stage of .Net Framework; Ado.net is actually upgraded version of Ado component | LINQ was introduced later stage with .Net 3.5 release |
Supports type checking at run-time; so we can find syntax errors at run-time only. | It supports type checking at compile-time , so we can debug and find syntax errors at compile-time. |
Apart from Ado.net objects we can write T-SQL query to work with database | LINQ provides common query syntax to work with any type of datasource. |