Using LINQ to manipulate data in DataSet/DataTable
2012-04-28 19:54
267 查看
Using LINQ to manipulate data in DataSet/DataTable
Have you tried a micro ORM for your data access?Click to read more…
LINQ (Language Integrated Query) provides a very easy and fast way to manipulate data that is cached in a DataSet. In .Net applications, DataSet represents a disconnected cache of data. Your application typically needs to search, filter thru this data in order
to display this data according to the need. DataView provides a very limited options
when it comes to creating a filtered view of the data. LINQ extends these capabilities to a greater extend.
A LINQ query opertaion consists of 3 actions (Ref:MSDN): obtain the data source, create the query and execute the query.
Any datasource that implements the IEnumerable(T) generic interface can be queried thru LINQ. So DataTable objects are good candidates to do any LINQ query opertions, we will see using the following examples, how some common tasks can be done using LINQ
queries.
For our example, we will consider that our DataSet has one(1) table with the following schema,
dtCustomer (CustomerID,CustomerName,Address,City,PostalCode,State,Country,PhoneNumer)
A simple select:
IEnumerable query = from customer in dtCustomer.AsEnumerable() select customer;
Till this point, we have the LINQ query ready, but not executed it yet, query is executed when we actually use it.
foreach (DataRow dr in query) { Console.WriteLine(dr.Field("CustomerName")); }
At this point, our query is executed and it prints the names of the customer.
We can also, get the resulset as a DataView by simply doing,
DataView view = query.AsDataView();
Most times, when we are dealing with DataSet/DataTable, data we will be creating a DataView as result of our LINQ query. ToList(), ToArray() methods are also very useful when you want to get your resultset
as a generic list or Array (Think AJAX/JSON!).
Lambda Expressions can be used as parameters to LINQ queries.
IEnumerable customers = query.Where(c => c.Field("CustomerName").Containes("John")); //All names that contain "John" foreach (DataRow cust in customers) { Console.WriteLine(cust.Field("CustomerName")); }
Simple Where Clause:
EnumerableRowCollection query = from customer in dtCustomer.AsEnumerable() where customer.Field("State") == "NJ" select customer; DataView njview = query.AsDataView();
Pretty simple, njview represents all customers that live in NJ.
You can extend the example to add more criteria to your where clause…
EnumerableRowCollection query = from customer in dtCustomer.AsEnumerable() where customer.Field("State") == "NJ" && customer.Field("PostalCode") == "08807" select customer; DataView njview = query.AsDataView();
It is useful to note that when you write your where clause, you leverage the power of your C# (or VB.Net) language features to search and filter your resultset using LINQ.
So, A SQL where clause is
where customer.Field("State") == "NJ" where customer.Field("State") != "NJ"
A SQL Like clause is
where customer.Field("CustomerName").Containes("John")
Skip and Take allows to get the skip the first n rows or get the top n rows as a result of the query.\
EnumerableRowCollection query = (from customer in dtCustomer.AsEnumerable() where customer.Field("State") == "NJ" select customer).Skip(3); EnumerableRowCollection query = (from customer in dtCusomter.AsEnumerable() where customer.Field("State") == "NJ" select customer).Take(3);
Simple ORDER BY clause:
EnumerableRowCollection query = from customer in dtCustomer.AsEnumerable() orderby customer.Field("CustomerName") select customer;
Above query, gets the result order by customer name (ascending is default). And if you want it by descending order,
EnumerableRowCollection query = from customer in dtCusomter.AsEnumerable() orderby customer.Field("CustomerName") descending select customer;
Reverse ORDER:
EnumerableRowCollection query = (from customer in dtCustomer.AsEnumerable() orderby customer.Field("CustomerName") select customer.Reverse();
Simple JOIN Operator:
var customers = ds.Tables["Customer"].AsEnumerable(); var orders = ds.Tables["Order"].AsEnumerable(); var query = from customer in customers join order in orders on order.Field("CustomerID") equals customer.Field("Customer") into custOrders select custOrders;
All the examples given above is just the tip of iceberg on what you can do with LINQ and Lambda expressions. There is tons of samples and articles available on Internet on this, if you are looking for a way to simplyfy and speed up your business processing
logic in your middle tier, LINQ is something you need to adopt!
Cheers!
相关文章推荐
- how to select data in multiple datatables after using join,group by and order by in Linq
- using an enumerable built-in Ruby to access and manipulate nested data
- LINQ to SQL系列Part 6 - Retrieving Data Using Stored Procedures
- LINQ系列:LINQ to DataSet的DataTable操作
- Linq to DataTable 解决DataSet不支持System.nullable
- How to use insert or retrieve data by using Core Data in iOS
- Preview Word files (docx) in HTML using ASP.NET, OpenXML and LINQ to XML
- using iscroll.js and iscroll jquery plugin in android webview to scroll div and ajax load data.
- Convert a DataReader to DataTable in ASP.NET
- Using the HTMLControl Class in Adobe AIR to parse HTML as a data source
- Preview Word files (docx) in HTML using ASP.NET, OpenXML and LINQ to XML
- LINQ to DataSet的DataTable操作
- To Search Data in Multiple Columns using 'IN' Clause
- ScottGu之博客翻译-第6部分-用存储过程检索数据-LINQ to SQL (Part 6 - Retrieving Data Using Stored Procedures)
- Linq&Lumda---LINQ to DataSet的DataTable操作
- LINQ系列:LINQ to DataSet的DataTable操作
- using JS to control two select(html),the data can be loaded from database and XML,and show in the select
- 将LINQ To DataSet 传回之对象集转成DataTable的函数
- LINQ TO DATATABLE/DATASET基本操作之-简单查询
- How To Generate An XML File As A Target Datastore Using ODI In An Integration Interface ? [ID 454268