Ankur Mukherjee
Ankur Mukherjee

Reputation: 3867

How to Run queries on datasets

I wish to run query on datasets.

Is it possible.

If yes please give me an example and if not please suggest alternative solution.

Upvotes: 1

Views: 1220

Answers (3)

brendan
brendan

Reputation: 30006

You can't run full SQL queries on DataSets but there are a couple of options:

(1) You can filter them using the DataSet.Select() method:

http://msdn.microsoft.com/en-us/library/det4aw50.aspx

DataTable table = DataSet1.Tables["Orders"];
// Presuming the DataTable has a column named Date.
string expression;
expression = "Date > #1/1/00#";
DataRow[] foundRows;

// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression);

(2) You can use LINQ to DataSet.

DataTable orders = ds.Tables["SalesOrderHeader"];

var query =
    from order in orders.AsEnumerable()
    where order.Field<bool>("OnlineOrderFlag") == true
    select new
    {
        SalesOrderID = order.Field<int>("SalesOrderID"),
        OrderDate = order.Field<DateTime>("OrderDate"),
        SalesOrderNumber = order.Field<string>("SalesOrderNumber")
    };

foreach (var onlineOrder in query)
{
    Console.WriteLine("Order ID: {0} Order date: {1:d} Order number: {2}",
        onlineOrder.SalesOrderID,
        onlineOrder.OrderDate,
        onlineOrder.SalesOrderNumber);
}

More examples located at : http://msdn.microsoft.com/en-us/library/bb387004.aspx

Upvotes: 3

Paul Sasik
Paul Sasik

Reputation: 81557

There is also a 3rd party library that allows nearly complete SQL querying on datasets. It's not free though.

Upvotes: 0

Coding Flow
Coding Flow

Reputation: 21881

You can use LINQ to DataSet. There are plenty of general examples here.

Upvotes: 4

Related Questions