DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • The Features of C# 9 That Will Make Your Life Easier [Snippets]
  • Python Memo 2: Dictionary vs. Set
  • Creating a Business Rule Engine Using Dynamic Expression Predicates With C#
  • Building a High-Throughput Distributed Sequence Generator Using the Hi-Lo Algorithm

Trending

  • Why Your Test Automation Is Always Behind the Code And the Architecture That Fixes It
  • 5 Common Security Pitfalls in Serverless Architectures
  • From 24 Hours to 2 Hours: How We Fixed a Broken BI System With Apache Airflow
  • When One MVP Is Really Four Systems: A Better Way to Plan Multi-Role Apps
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Return Dictionary As a Result From a LINQ Query in C#?

How to Return Dictionary As a Result From a LINQ Query in C#?

By 
Senthil Kumar user avatar
Senthil Kumar
·
Feb. 13, 13 · Interview
Likes (0)
Comment
Save
Tweet
Share
53.3K Views

Join the DZone community and get the full member experience.

Join For Free


This article will provide a code snippet and explains how to return Dictionary as result from a LINQ Query in C#.

There are times when you want to retrieve only the ID(distinct) and the name from the database table using LINQ . In scenarios like this , one can use the ToDictionary method to place the necessary properties to the dictionary and return them.

Below is a sample sourecode demonstrating the usage of ToDictionary method in LINQ Query


public class BlockbusterMovie
{
public string Name { get; set; }
public int ID { get; set; }
}
public class BlockbusterMovies : List<BlockbusterMovie>
{
public BlockbusterMovies()
{
Add(new BlockbusterMovie { Name = "Vishwaroopam", ID = 1 });
Add(new BlockbusterMovie { Name = "Endhiran", ID = 2 });
Add(new BlockbusterMovie { Name = "Thuppaki", ID = 3 });
Add(new BlockbusterMovie { Name = "Mankatha", ID = 4 });
}
}


The BlockbusterMovies class has the collection of movies which is used in the below code snippet to return the dictionary based on the ID and Name.


private void Form1_Load(object sender, EventArgs e)

{

List<BlockbusterMovie> movies = new BlockbusterMovies();

var LstMovies = movies.ToDictionary(Field => Field.ID, mc => mc.Name);

}



How to return Dictionary as result from a LINQ Query in C# ?



csharp Database Dictionary (software)

Published at DZone with permission of Senthil Kumar. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Features of C# 9 That Will Make Your Life Easier [Snippets]
  • Python Memo 2: Dictionary vs. Set
  • Creating a Business Rule Engine Using Dynamic Expression Predicates With C#
  • Building a High-Throughput Distributed Sequence Generator Using the Hi-Lo Algorithm

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook