Dynamic query with linq in IDictionary <string, object>?

This question is similar to this question , but not exactly the same.

Can I make a dynamic query in the linq-to-sql data context and return the result as IDictionary <string, object> where the key is the column name?

A bit like (doesn't compile, but illustrates intent)

IDictionary<string, object> data = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
data = db.ExecuteQuery<IDictionary<string, object>(queryString, params).ToDictionary(
    k=>nameOfColumn,
    k=>k
)

      

Obviously I left the map completely in data = db.ExecuteQuery ...
I have a feeling that executeQuery is not what I should be using as this one is already trying to do a mapping to an object. For this specific use case, I don't want that.

+2


source to share


1 answer


You will need to specify the type of object to materialize; What about:



var data = db.ExecuteQuery<SomeType>(queryString, params)
          .ToDictionary(st => st.SomeKey);

      

+2


source







All Articles