Skip to main content
Email Password   helpLost your password?

Screenshot - demo.jpg

Introduction

In this article, I will speak about LINQ and Compact Framework 3.5. I've written a simple example to demonstrate how we can use LINQ and Dataset to manipulate data.

Background

What is LINQ? LINQ is a codename for the set of extensions to the .NET Framework that encompass language-integrated data query, set, and transform operations. It includes extensions to the C# and Visual Basic languages with native language syntax for queries. It also provides class libraries to take advantage of these capabilities. With the release of Compact Framework 3.5 and Visual Studio "Orcas" Beta 1, LINQ extensions are now also available for mobile development.

About the sample

In this example, I will demonstrate a simple application that has one form. It displays and searches countries' information in a grid. For an illustration of how to work with LINQ and Dataset, the application will retrieve data from the SQL Server Compact Edition file demo.sdf. The application uses Typed Data Adapter as the Data Access Layer and DataTable as the data type.

Screenshot - ds.jpg

LINQ in action

In the code below, I am retrieving data from a database and filling in the Countries DataTable. After that, just for illustration, I am using LINQ to sort and display the data in a grid through the bindingSource object.

///Loading data from demo.sdf to Countries table

private void LoadData()
{
    ///Get Data

    Countries = dal.GetData();

    ///Building LINQ Query

    var query = from r in Countries.AsEnumerable()
        orderby r.Field<string>("Name") ascending
        select new 
    { 
        Code = r.Field<string>("Code"), Name = r.Field<string>("Name") 
    };

    ///Binding data

    bindingSource1.DataSource = query;
}

In the code below, I am using LINQ to search data in DataTable.

///Building LINQ Query

var query = from r in Countries.AsEnumerable()
    where r.Field<string>("Name").ToLower().Contains(txtSearch.Text.ToLower())
    orderby r.Field<string>("Name") ascending
    select new 
    { 
        Code = r.Field<string>("Code"), Name = r.Field<string>("Name") 
    };

///Binding data

bindingSource1.DataSource = query;

Conclusion

As you can see, LINQ is very easy and intuitive to use. Its syntax is quite similar to that of SQL and it is a part of the C# and Visual Basic languages. We can use it to manipulate data in many scenarios.

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
QuestionHow about the update dataRow in LINQ? Pin
agskid
18:21 1 Jul '09  
AnswerRe: How about the update dataRow in LINQ? Pin
Oleg Levin
19:13 1 Jul '09  
GeneralRe: How about the update dataRow in LINQ? Pin
agskid
4:19 2 Jul '09  
AnswerRe: How about the update dataRow in LINQ? Pin
Oleg Levin
5:41 2 Jul '09  
GeneralHi there Pin
Sebulba_se
6:29 10 Sep '08  
GeneralRe: Hi there Pin
Oleg Levin
8:00 10 Sep '08  
QuestionAsEnumerable() does not exist. Pin
zhenyulu2003
13:04 17 Aug '07  
AnswerRe: AsEnumerable() does not exist. Pin
Oleg Levin
9:37 18 Aug '07  
GeneralRe: AsEnumerable() does not exist. Pin
zhenyulu2003
3:56 20 Aug '07  
GeneralCompact Framework Pin
Pamela1984
22:38 12 Jul '07  
GeneralRe: Compact Framework Pin
Oleg Levin
3:56 13 Jul '07  
GeneralRe: Compact Framework Pin
Pamela1984
4:17 15 Jul '07  
GeneralRe: Compact Framework Pin
Oleg Levin
9:32 15 Jul '07  
GeneralRe: Compact Framework [modified] Pin
Pamela1984
20:43 17 Jul '07  
QuestionLINQ Pin
gagliapas
14:43 19 Jun '07  
AnswerRe: LINQ Pin
Oleg Levin
21:02 19 Jun '07  
Generalnice Pin
Sacha Barber
23:37 14 Jun '07  
GeneralRe: nice Pin
Oleg Levin
7:14 15 Jun '07  
GeneralThanks Pin
merlin981
6:21 14 Jun '07  


Last Updated 14 Jun 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009