Is there a way of changing what property a Linq statement does a Where and an OrderBy please?
I have the following example function that I am trying to make more flexible:
public static List<SADatastore.SA_Items> BuildResourcesModel2(
int Take = 10000,
int Skip = 0,
string filterBy = null,
string filterString = null,
string sortBy = "ItemID",
string sortOrder = "asc"
) {
List<SADatastore.SA_Items> theAnswer = new List<SA_Items>();
using (SADatastoreEntities db = new SADatastoreEntities(Properties.Settings.Default.ConnectionString)) {
theAnswer = db.SA_Items
.Where(x => x.SA_Categories.Description.ToLower().Contains(filterString.ToLower()))
.OrderBy(x=>x.ItemID)
.Take(Take)
.Skip(Skip)
.ToList();
}
return theAnswer;
}
As you can see, as it's written at the moment, it is always doing the where based on the Category Description and Ordering Ascending based on ItemID
Is there a way to rewrite this in a dynamic way so if it's called with the parameters
filterBy = "Manufacturer"
filterString "Ford"
sortBy = "Manufacturer"
sortOder = "Desc"
It would dynamically change the Where and Order clauses?
Any help would be greatly appreciated!