Click here to Skip to main content
11,581,515 members (63,151 online)
Click here to Skip to main content

Usage of LINQ and Datasets in Compact Framework 3.5

, 14 Jun 2007 CPOL 71.4K 1.5K 42
Rate this:
Please Sign up or sign in to vote.
Introduction to working with LINQ and DataSets in Compact Framework 3.5 Beta 1

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

  • 14 June, 2007 -- Original version posted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Oleg Levin
Software Developer SoftSolutions
Israel Israel
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 4 Pin
suhaslive12-Oct-11 17:10
membersuhaslive12-Oct-11 17:10 
QuestionHow about the update dataRow in LINQ? Pin
agskid1-Jul-09 17:21
memberagskid1-Jul-09 17:21 
AnswerRe: How about the update dataRow in LINQ? Pin
Oleg Levin1-Jul-09 18:13
memberOleg Levin1-Jul-09 18:13 
GeneralRe: How about the update dataRow in LINQ? Pin
agskid2-Jul-09 3:19
memberagskid2-Jul-09 3:19 
AnswerRe: How about the update dataRow in LINQ? Pin
Oleg Levin2-Jul-09 4:41
memberOleg Levin2-Jul-09 4:41 
GeneralHi there Pin
Sebulba_se10-Sep-08 5:29
memberSebulba_se10-Sep-08 5:29 
GeneralRe: Hi there Pin
Oleg Levin10-Sep-08 7:00
memberOleg Levin10-Sep-08 7:00 
QuestionAsEnumerable() does not exist. Pin
zhenyulu200317-Aug-07 12:04
memberzhenyulu200317-Aug-07 12:04 
AnswerRe: AsEnumerable() does not exist. Pin
Oleg Levin18-Aug-07 8:37
memberOleg Levin18-Aug-07 8:37 
GeneralRe: AsEnumerable() does not exist. Pin
zhenyulu200320-Aug-07 2:56
memberzhenyulu200320-Aug-07 2:56 
GeneralCompact Framework Pin
Pamela198412-Jul-07 21:38
memberPamela198412-Jul-07 21:38 
GeneralRe: Compact Framework Pin
Oleg Levin13-Jul-07 2:56
memberOleg Levin13-Jul-07 2:56 
GeneralRe: Compact Framework Pin
Pamela198415-Jul-07 3:17
memberPamela198415-Jul-07 3:17 
GeneralRe: Compact Framework Pin
Oleg Levin15-Jul-07 8:32
memberOleg Levin15-Jul-07 8:32 
GeneralRe: Compact Framework [modified] Pin
Pamela198417-Jul-07 19:43
memberPamela198417-Jul-07 19:43 
QuestionLINQ Pin
gagliapas19-Jun-07 13:43
membergagliapas19-Jun-07 13:43 
AnswerRe: LINQ Pin
Oleg Levin19-Jun-07 20:02
memberOleg Levin19-Jun-07 20:02 
Generalnice Pin
Sacha Barber14-Jun-07 22:37
memberSacha Barber14-Jun-07 22:37 
GeneralRe: nice Pin
Oleg Levin15-Jun-07 6:14
memberOleg Levin15-Jun-07 6:14 
GeneralThanks Pin
merlin98114-Jun-07 5:21
membermerlin98114-Jun-07 5:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 14 Jun 2007
Article Copyright 2007 by Oleg Levin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid