Trinitrotoluene
Trinitrotoluene

Reputation: 1458

C# LINQ to Dictionary

I have a set of LINQ results as follows:

var dictionaryHere = db.tbl_user_pass_list
      .Where(e => e.Usage1 == "Domain Administrator");

This returns a number of properties, of which I need the AdminUsername and AdminPassword and Company.

I have a class setup for this:

public class AdminUsernamePassword
{
    public string AdminUsername { get; set; }
    public string AdminPassword { get; set; }
}

What I want to generate is a dictionary where the Key is company, and the value contains AdminUsername and AdminPassword. I've got as far as this:

 Dictionary<string, AdminUsernamePassword> dictionary = dictionaryHere.ToDictionary(o => o.Company,

I'm not sure how to complete this though to get my two other values in as the AdminUsernamePassword type. Any help appreciated!

Upvotes: 2

Views: 107

Answers (1)

germi
germi

Reputation: 4658

You just need to specify your Value of the KeyValuePair. You already have the key:

dictionaryHere.ToDictionary(o => o.Company, [...])

You just need to create your Value:

dictionaryHere.ToDictionary(o => o.Company, 
                            x => new AdminUserPassword 
                            { 
                              AdminUserName = x.UserName, 
                              AdminPassword = x.Password 
                            });

Upvotes: 5

Related Questions