This article provides an introduction to employing LINQ to SQL within a Windows Forms application.
Let start the LINQ with simple Example, This is an example of a very simple LINQ to SQL statement:
public void SimpleQuery()
{
DataClasses1DataContext dc = new DataClasses1DataContext();
var q = from a in dc.GetTable()
select a;
dataGridView1.DataSource = q;
}
In the example, an instance of the data context is created and then a query is formed to get all of the values in the table. once the query runs, the result is used as the data source of a datagridview control and the results are displayed in the grid.
Now We can try this LINQ by using Where Clause,
public void SimpleQuery2()
{
DataClasses1DataContext dc = new DataClasses1DataContext();
var q = from a in dc.GetTable()
where a.CustomerID.StartsWith("A")
select a;
dataGridView1.DataSource = q;
}
Now we can Try using the ORDER BY Query in LINQ:
public void SimpleQuery3()
{
DataClasses1DataContext dc = new DataClasses1DataContext();
var q =
from a in dc.GetTable()
where a.CustomerID.StartsWith("A")
orderby a.OrderDate ascending
select a;
dataGridView1.DataSource = q; }
The article shows some simple examples of LINQ to SQL, from it you can see how easy it is to query against single and related tables and to write filtered queries.
|
| Author: Prasoon Kumar 29 Jun 2009 | Member Level: Silver Points : 1 |
Hi Good one !!
There are few keywords/clauses that are not supported across
DataSet while querying dataset.
it is not related to LINQ to SQL but to LINQ to OBJECTS.
Regards Prasoon
|