views:

348

answers:

1

This question is similar to this question but not quite the same.

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

A bit like so (doesn't compile, but illustrates the intention)

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'm totally off the map in the data = db.ExecuteQuery...
I have the feeling that the executeQuery is not what I'm ought to use as this one already tries to do the mapping to an object. For this particular use case I don't want that.

+1  A: 

You'll need to include an object type to materialize; how about:

var data = db.ExecuteQuery<SomeType>(queryString, params)
          .ToDictionary(st => st.SomeKey);
Marc Gravell
Wouldn't that make Linq try to parse the values to the SomeType?
borisCallens
Yes... but what is the alternative - `object`? If you don't have typed data, then LINQ may not be the best option; just use ADO.NET and SqlCommand.ExecuteReader
Marc Gravell
Indeed, I in the meanwhile already did that.Maybe I should research if I can make my typed data somehow plug in on Linq's parsing.
borisCallens