Dynamic Properties in LinqConnect
Posted: Tue 26 Apr 2011 11:26
My code attempted some reflection in a Linq query. Not surprisingly this resulted in a NotSupportedException. Unfortunately I have no appealing alternatives.
My current code:
A list of tuples is entered as a parameter. The first item is the name of a property and the second is the desired value. Somehow I need to transform this list into where clauses for the query. Obviously this is not the way.
One alternative I've considered was simply creating a where clause for each property in the entity, but that would leave me with a giant set of if statements, which does not really leave me with fuzzy happy feelings.
Another alternative suggested by someone, was to write an extension of sorts to provide the sql for this construction. I'm not really familiar with LinqConnect however and would even know where to begin.
Which would be the best approach to solve this problem? Or is there an other way I'm overlooking?
My current code:
Code: Select all
public static IEnumerable Find(int start, int limit, IEnumerable> filter)
{
var context = new DataContext();
var query = context.OrderViews;
foreach (var tuple in filter)
{
query = query.Where(o => tuple.Item2.Equals(o.GetType().GetProperty(tuple.Item1).GetValue(o, null)));
}
return query.Skip(start).Take(limit);
}
One alternative I've considered was simply creating a where clause for each property in the entity, but that would leave me with a giant set of if statements, which does not really leave me with fuzzy happy feelings.
Another alternative suggested by someone, was to write an extension of sorts to provide the sql for this construction. I'm not really familiar with LinqConnect however and would even know where to begin.
Which would be the best approach to solve this problem? Or is there an other way I'm overlooking?