Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# List<object> to Dictionary<key, <object>>

I am a beginner c# developer and I need to create a dictionary of object from my list. First, let me define my object as Person.

public class Person 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

Now I have a list of my person object. List<Person> How to query it in LinQ to convert it to a Dictionary of Person from my list? My desired output is:

Dictionary<key, <Person>>

Where key is an incrementing integer per Person object.. Any help is appreciated. Thank you.

I found this code online but it works with List<string>

List<string> List1
var toDict = List1.Select((s, i) => new { s, i })
             .ToDictionary(x => x.i, x => x.s)
like image 582
super-user Avatar asked Apr 14 '16 01:04

super-user


People also ask

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in simple words?

C Introduction C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is %d in C programming?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.

Why is C named so?

Because a and b and c , so it's name is C. C came out of Ken Thompson's Unix project at AT&T. He originally wrote Unix in assembly language. He wrote a language in assembly called B that ran on Unix, and was a subset of an existing language called BCPL.


2 Answers

One most straight forward way would be to use the int key as key like this:

List<Person> List1 = new List<Person>();
int key = 0; //define this for giving new key everytime
var toDict = List1.Select(p => new { id = key++, person = p })
    .ToDictionary(x => x.id, x => x.person);

The key is the lambda expression:

p => new { id = key++, person = p }

Where you create an anonymous object having id and person properties. The id is incremental key while the person is simply the element of your List<Person>

If you need to use the Person's Id instead, simply use:

List<Person> List1 = new List<Person>();
var toDict = List1.Select(p => new { id = p.Id, person = p })
    .ToDictionary(x => x.id, x => x.person);
like image 148
Ian Avatar answered Sep 20 '22 15:09

Ian


You were almost there, just change variable type from List<string> to List<Person> and you are good to go. You can use your LINQ query as is, example:

List<Person> persons = new List<Person>();

var p1 = new Person();
p1.Name = "John";
persons.Add(p1);

var p2 = new Person();
p2.Name = "Mary";
persons.Add(p2);

var toDict = persons.Select((s, i) => new { s, i })
             .ToDictionary(x => x.i, x => x.s);

However, while I don't have anything against LINQ, in this particular case a much more readable approach is using a regular loop like this:

var result = new Dictionary<int, Person>();
for (int i = 0; i < persons.Count; i++)
{
    result.Add(i, persons[i]);
}

Jon Skeet suggests yet another way of doing it, using Enumerable.Range, which I tested and it works perfectly:

var toDict = Enumerable.Range(0, persons.Count)
             .ToDictionary(x => x, x => persons[x]);
like image 31
Neolisk Avatar answered Sep 18 '22 15:09

Neolisk