Skip to content
Advertisement

Using SqlQuery<Dictionary> in Entity Framework 6

I’m trying to execute a SQL query in EF 6. The select query returns two string columns, e.g. select 'a', 'b', and can have any number of rows.

I’d like to map the result to a dictionary, but I can’t get ride of the following error.

Error 1 Cannot implicitly convert type ‘System.Data.Entity.Infrastructure.DbRawSqlQuery>’ to ‘System.Collections.Generic.Dictionary’

This is the code:

I’ve tried various select and ToDictionary after the query, but none of them worked.

Advertisement

Answer

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:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement