2

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!

4
  • 6
    .ToDictionary(o => o.Company, x => new AdminUserPassword(x.UserName, x.Password)? I assumed a ctor with username and password to keep it short. Commented Apr 23, 2015 at 8:50
  • 1
    @germi Should have been an answer. Commented Apr 23, 2015 at 8:52
  • @Magnus right, I added it as an answer Commented Apr 23, 2015 at 8:54
  • Not posting this as an answer since it doesn't answer the question, but be sure that the Adminpassword is properly secured: encrypt it while it's in the Dictionary, and don't decrypt it until you actually need it.. Commented Apr 23, 2015 at 9:00

1 Answer 1

5

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 }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.