I'm trying to build a function to dynamically generate different queries based on some parameters. I am a bit confused on LINQ syntax and I'm not sure if I'm doing it right.
The set of String type parameters are "search" (for search textbox value), "searchfield" (what to search), "limit_begin", "limit_end" for how many rows and where to start. "order_by" for which field to order by. "order_sort" for which way to sort.
I found this 'getpropertyvalue' reflection function on stackoverflow before, I'm hoping it does what I'm intending based on my own interpretation.
private static object GetPropertyValue(object obj, string property)
{
System.Reflection.PropertyInfo propertyInfo = obj.GetType().GetProperty(property);
return propertyInfo.GetValue(obj, null);
}
if (order_sort == "ASC")
{
(from a in entities.UserTable
where GetPropertyValue(a, searchfield).ToString().Contains(search)
select a)
.OrderBy("a." + order_by)
.Skip(Convert.ToInt32(limit_begin))
.Take(Convert.ToInt32(limit_end))
.ToList();
}
else if (order_sort == "DESC")
{
(from a in entities.UserTable
where GetPropertyValue(a, searchfield).ToString().Contains(search)
select a)
.OrderByDescending("a." + order_by)
.Skip(Convert.ToInt32(limit_begin))
.Take(Convert.ToInt32(limit_end))
.ToList();
}
I'm getting an error on "Orderby" line, and VS2008 highlights it in red saying the type of argument cannot be inferred from the usage.