Windows Phone Mango Local Database(SQL CE): Database Queries with LINQ
published on: 10/5/2011 | Views: N/A | Tags: LocalDB
by WindowsPhoneGeek
This is the 11th post from the "Windows Phone Mango Local Database" series of short posts that will cover all you need to know in order to get started using a Local Database in Windows Phone 7.1 Mango. This time I am going to talk about database queries with LINQ when working with a Windows Phone 7.1 Mango local database.
Here is what else is included in this series:
- Windows Phone Mango Local Database(SQL CE): Introduction
- Windows Phone Mango Local Database(SQL CE): Linq to SQL
- Windows Phone Mango Local Database(SQL CE): [Table] attribute
- Windows Phone Mango Local Database(SQL CE): [Column] attribute
- Windows Phone Mango Local Database(SQL CE): [Association] attribute
- Windows Phone Mango Local Database(SQL CE): [Index] attribute
- Windows Phone Mango Local Database(SQL CE): Database mapping
- Windows Phone Mango Local Database(SQL CE): DataContext
- Windows Phone Mango Local Database(SQL CE): Connection Strings
- Windows Phone Mango Local Database(SQL CE): Creating the Database
- Windows Phone Mango Local Database(SQL CE): Database Queries with LINQ
- Windows Phone Mango Local Database(SQL CE): How to Insert data
- Windows Phone Mango Local Database(SQL CE): How to Update data
- Windows Phone Mango Local Database(SQL CE): How to Delete data
What is a Database query?
On Windows Phone, Language Integrated Query (LINQ) is used to query the database. LINQ is used to represent the connection between the objects and the real data. Queries in LINQ to SQL use the same syntax as queries in LINQ.
Because objects referenced in LINQ to SQL queries are mapped to records in a database, LINQ to SQL differs from other LINQ technologies in the way that queries are executed. A typical LINQ query is executed in memory at the application layer. With LINQ to SQL, using the object-relational capabilities of the runtime, each LINQ query is translated to Transact-SQL and then executed directly in the database. This can yield a performance gain for queries such as selecting a few records out of a large database.
For reference you can also take a look at the full MSDN documentation.
You can also take a look at our previous post: Windows Phone Mango Local Database(SQL CE): Linq to SQL
How to Select Data?
In the following example, a DataContext object named "CountryDataContext" is queried with LINQ to SQL and the results are placed into an IList collection of Country objects named countryList .
NOTE: In the next example we will use the "CountryDataContext" data context explained in the previous posts of this series.
Example 1 - Select all country records from the database:
private IList<Country> GetCountries()
{
IList<Country> countryList = null;
using (CountryDataContext context = new CountryDataContext(ConnectionString))
{
IQueryable<Country> query = from c in context.Countries select c;
countryList = query.ToList();
}
return countryList;
}
Example 2 - Select all country records from the database where the country name starts with "U'":
private IList<Country> GetCountriesStartingWithU()
{
IList<Country> countryList = null;
using (CountryDataContext context = new CountryDataContext(ConnectionString))
{
IQueryable<Country> query =
from c in context.Countries
where c.Name.StartsWith("U")
select c;
countryList = query.ToList();
}
return countryList;
}
In this article I talked about database queries with LINQ when working with a Windows Phone 7.1 Mango local database. Stay tuned for the rest of the posts.
You may also find interesting the following articles:
- Windows Phone Mango Local Database(SQL CE): Introduction
- Using SqlMetal to generate Windows Phone Mango Local Database classes
- A Simple ToDo List: Persisting Data with the Sterling NoSQL Database
- All about WP7 Isolated Storage series
You can also follow us on Twitter @winphonegeek
Comments
posted by: Matt Brown on 10/23/2011 7:27:13 AM
I appreciate this series (as I do all of your series). I'm understanding basic queries such as shown here and have used a couple in my apps, but one thing I can't figure out is how to group data in a listbox presentation.
Say, I have a grocery list app, and I want to group all items by aisle with the name ascending, so that the output would look something like:
Baked Goods (Aisle Name) - Bread (Item Name) - Pastries
Canned Goods - Broccoli Soup - Chicken Soup - Tomato Soup
Any suggestions or pointers? Thanks.
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
