Click here to Skip to main content
13,297,513 members (64,653 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 12 Feb 2009

Collection Initializers and Query Expressions for Dictionary Objects

, 12 Feb 2009
Rate this:
Please Sign up or sign in to vote.
This article explains the syntax to use collection initializers and query expressions for Dictionary objects in C# 3.0.


This article explains the syntax to use collection initializers and query expressions for Dictionary objects in C# 3.0.


I was learning the new features of C# 3.0. Though the use of collection initializers and query expressions are simple and straight forward in most cases, their use with Dictionary objects are not well documented. Hence, this article gives a beginner level approach to understanding collection initializers and query expressions with Dictionary objects.

Using the Code

In the below block of code, a Dictionary object (currencyCollection) has been defined. The Key holds an id, and the Value holds different currencies.

//Collection Initializers
Dictionary<int, string> currencyCollection = new Dictionary<int, string> {
                {1,"Indian Rupee"},
                {2, "United States Dollar"},
                {3, "Euro"},
                {4, "British Pound"},
                {5, "Australian Dollar"},
                {6, "Japanese Yen" },
                {7,"Indian Rupee"}

In the next step, I'm querying the currency "Indian Rupee" using query expressions. This expression will find out the entries for "Indian Rupee".

//Query Expressions
var query = from c in currencyCollection
            where (c.Value.Equals("Indian Rupee"))
            select c;

Now the variant query has the objects after filtering only "Indian Rupee". I use a simple foreach to display the results.

//Iterate through the dictionary and print
foreach (var ky in query)
    Console.WriteLine("{0}, {1}", ky.Key.ToString(), ky.Value); 

Points of Interest

Two things which I learnt were as follows:

  1. How to use collection initializers for Dictionary objects.
  2. How to use query expressions for Dictionary objects.


  • Created: 2009-Feb-11


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


About the Author

Arun George John
Team Leader
India India
Arun George John is a technology enthusiast. He has been involved with .Net since 2002 and has worked on several challenging projects.

Apart from work, he is an amateur musician who can strike a note or two on a piano or an organ.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Qistoph13-Feb-09 0:17
memberQistoph13-Feb-09 0:17 
Nothing new. It's just simple and trivial usage of LINQ.
GeneralThanks Pin
jnovax12-Feb-09 17:50
memberjnovax12-Feb-09 17:50 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 12 Feb 2009
Article Copyright 2009 by Arun George John
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid