Dynamic selection of fields
I'm attempting to write a linq extension method but I'm missing some
knowledge. I'm not even sure I know how to ask what I need, so please
consider the following:
public static class LinqExtensions
{
public class MyExtRet
{
WHAT_TYPE keys;
WHAT_TYPE vals;
}
public static IQueryable<T> MyExt<T, TKey>(this IQueryable<T> source,
Expression<Func<T, TKey>> keySelector)
{
List<MyExtRet> ret = new List<MyExtRet>();
foreach (var o in source.OrderBy(keySelector))
{
MyExtRet r = new MyExtRet();
r.keys = ; // how to dereference?
r.vals = ;
}
}
}
I want to be able to call this thusly (using EF):
TestContainer db = new TestContainer();
var MyList = db.SomeEntity
.Select(x => new { x.state, x.year, x.price })
.MyExt(x => new { x.state, x.year });
foreach (var o in MyList)
{
Console.WriteLine(o.keys.state);
Console.WriteLine(o.vals.price);
}
with the result that I get objects where the keys are separated from the
values.
so my question is: how do I dereference o, which contains some object the
properties of which I don't know, to select the values of all those
properties given to me as keys in the keySelector, and then select the
values of all those properties NOT given to me?
and furthermore, how do I return this stuff so it's IQueryable (I'm not
even sure how to declare the right return type)?
thanks for thinking about it
-- ekkis
No comments:
Post a Comment