简体   繁体   中英

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!

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 
                            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM