Using SqlQuery<Dictionary<string, string>> in Entity Framework 6
You can use SqlQuery
to directly populate an object if the object has a default constructor and property setters. The result can then be used to create a dictionary. For example:
public class QueryResult
{
public string A { get; set; }
public string B { get; set; }
}
// the colulmn/alias names need to match property names
string query = "SELECT column1 AS [A], column2 AS [B] FROM ..."
using (var db = new EFDbContext())
{
var perms = db.Database.SqlQuery<QueryResult>(query)
.ToDictionary(r => r.A, r => r.B);
}