I'm using DbLinq which should be the equivalent of Linq2SQL for this question. I'm need to generate a Linq2SQL query where I specify the columns I want returned at runtime. I can achieve that using the Dynamic Linq extension methods but I can't work out how to extract the result.
string someProperty = "phonenumber";
string id = "1234";
Table<MyClass> table = context.GetTable<MyClass>();
var queryResult = (from item in table where item.Id == id select item).Select("new (" + someProperty + ")");
The Linq expression generates the proper SQL of:
select phonenumber from mytable where id = '1234'
And in the debugger I can see the phonenumber value is sitting there in the Results View. The problem is I can't work out how to get the phonenumber value from the queryResult object? The type of queryResult is:
QueryProvider<DynamicClass1>
Edit: I discovered a way to do it but it seems very crude.
IEnumerator result = (from item in table where item.Id == id select item).Select("new (" + someProperty + ")").GetEnumerator();
result.MoveNext();
var resultObj = result.Current;
PropertyInfo resultProperty = resultObj.GetType().GetProperty(someProperty);
Console.WriteLine(resultProperty.GetValue(resultObj, null));
Perhaps someone knows a cleaner way?