views:

546

answers:

2

Is it possible to get a collection (Dictionary) out of an Entity Object? I need this in order to pass parts of the Properties of the object to a function that needs an IDictionary.

A: 

I don't think I fully understand your question. I don't know of any way of binding your database tables to IDictionary properties if that is what you are referring to, but you should be able to create whatever properties or methods you need to build a dictionary, and pass that. Is writing a business object method not an option for some reason?

slf
Example: the object has the properties a, b, c, x, y, z. And I need the properites x, y, z in a Dictionary as Keys and the values of x, y, z as values.
+1  A: 

Use:

Context.EntitySetName.AsEnumerable().ToDictionary(o => o.Key, o => o.Value);

Some detail on this:

ToDictionary is not supported in LINQ to Entities. This means that LINQ query string containing ToDictionary will compile, but will not execute, because the Entity Framework does not know how to translate them into SQL. Therefore, you have to execute the query on the database server first. So you have to project your set into a list first, which enumerates the entity set. AsEnumerable will do that. Obviously, if the entity set is large, you probably don't want to do this with the whole thing. Use a Where call or a LINQ query to reduce the result set to only the items you want in the dictionary first.

Craig Stuntz