Reputation: 10226
I have a table in SQL Server with 3 columns, a domain, a key and a value. I'd like to end up with a hash that I can use in dot-net. I'm using EF 4.0.
at first I tried:
List<Dictionary<string, string>> myHash = db.Registries
.Where(x => x.Domain == "Pricing")
.Select(x => new { Key = x.Key, Value = x.Value });
which complains:
Error 3 Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List>'. An explicit conversion exists (are you missing a cast?) C:\Users\ekkis\Documents\Visual Studio 2010\Projects\SkillScore\Website\Controllers\HomeController.cs 53 21 Website
so obviously the type of the select needs to be tweaked. I've messed with it for half an hour (I'm so useless!) without being able to make it work (God I miss IRC). help anyone?
Upvotes: 0
Views: 704
Reputation: 13574
I got interested in: "How to build a dictionary-of-dictionaries in a LINQ query".
Here's my humble attempt, which is untested because I don't a LINQ-DB, because I don't have IIS, because I can't intall IIS for "Genuine" reasons some might guess.
Please feel free to edit this code to fix it (if it's broke).
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args) {
var registries = FetchAllRegistries();
foreach ( KeyValuePair<string, Dictionary<string,string>> domain in registries ) {
Console.WriteLine("Domain: "+domain.Key);
foreach ( KeyValuePair<string,string> entry in domain.Value ) {
Console.WriteLine(" "+entry.Key+" "+entry.Value);
}
}
}
private static Dictionary<string, Dictionary<string, string>> FetchAllRegistries() {
return db.Registries
.GroupBy(x => x.Domain)
.Select(g => g.ToDictionary(
d => d.Domain,
d => d.ToDictionary(
kv => kv.Key,
kv => kv.Value
)
))
;
}
private static Dictionary<string, Dictionary<string, string>> FetchRegistry(string domainName) {
return db.Registries
.Where(x => x.Domain == domainName)
.ToDictionary(
x => x.Key
, x => x.Value
);
}
}
}
And Forgive me: These queries just add credence to my opinion that LINQ is sooooo ugly that it's posteriour should be shaved, and it should be taught to walk backwards. True: the "long-hand" version IS a LOT more verbose, BUT it's also a LOT more self-explanatory. I suppose my perspective is that "LINQ is grand. Shame about the STEEEEEEP learning curve".
And, yes please... lets all go back to Perl cgi-scripts. Bless me father, for I cannot inheret... I'm in love with a commoner. Sigh.
Cheers. Keith.
Upvotes: 0
Reputation: 160852
Sounds like this is what you need:
List<Dictionary<string, string>> myHash = db.Registries
.GroupBy(x => x.Domain)
.Select(g => g.ToDictionary(x => x.Key,
x => x.Value))
.ToList() ;
This creates a separate dictionary for each domain and puts them all in a list. This is not entirely useful though, since you lose the information about the domain name - maybe a Dictionary<string, Dictionary<string,string>>
would be more what you want (having the domain name as key of the outer dictionary).
A dictionary for a single domain you can get by using ToDictionary()
directly:
var pricingDictionary = db.Registries
.Where(x => x.Domain == "Pricing")
.ToDictionary( x => x.Key,
x => x.Value);
Note that pricingDictionary is of type Dictionary<string, string>
so if you need the full type you can do:
Dictionary<string, string> myHash = db.Registries
.Where(x => x.Domain == "Pricing")
.ToDictionary( x => x.Key,
x => x.Value);
Upvotes: 3