I am trying to create a function for dynamically generating various queries based on some parameters. I am a little confused by LINQ syntax, and I'm not sure if I am doing this correctly.
A set of parameters of type String is "search" (for the value of the search text field), "searchfield" (what to look for), "limit_begin", "limit_end" for the number of lines and where to start. "order_by" for which the field is being ordered. "order_sort" for which to sort.
I discovered this "getpropertyvalue" reflection function in stackoverflow before, I hope this does what I intend, 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();
}
"Orderby", VS2008 , , .