Reputation: 875
I'm trying to return new data to jTable from my entity framework db context. I want to be able to sort this data, but I'm not having any luck. I'm passing in a variable into the orderby statement of my linq query, but not matter what I do - it doesn't sort. If I type out the value of that string, it works - so I'm wondering if it is possible to add string variables into linq queries? From what else I've seen on here, it seems like a common issue.
I'm in VS 2010, .net 4.0, MVC 3, and I've already added and tried to get Dynamic Linq to work and have it it added using System.Data.Entity;
[HttpPost]
public JsonResult WineList(string jtSorting = null, int ProducerID = 0)
{
try
{
jtSorting = "wine." + jtSorting.Replace("ASC", "ascending").Replace("DESC", "descending");
List<Wine> wines = db.Wines.Where(w => w.Active == true).Where(w => w.ProducerID == ProducerID).ToList();
//have to do this so we don't get circular references between producers and wines
var q = (from w in wines
let wine = new
{
WineID = w.WineID,
Producer = w.Producer.Name,
VarType = w.VarType.Name,
Vintage = w.Vintage.Name,
Name = w.ShortName,
App = w.App.Name
}
orderby(jtSorting)
select wine);
//var x = from w in q
// orderby jtSorting
// select w;
return Json(new { Result = "OK", Records = q });
}
catch (Exception ex)
{
return Json(new { Result = "ERROR", Message = ex.Message });
}
}
If anyone has a better way to deal with jtable, I'm open to that too! Thanks
Upvotes: 0
Views: 2356
Reputation: 2656
I'm also using jTable in a project. Ricci Gian Maria summed up this "OrderBy" problem very well here.
The solution to the above problem is really simple, you should only start thinking on how the OrderBy LINQ operator works, it basically accepts a lambda that express the ordering criteria, for a customer object it can be something like OrderBy( c=> c.CustomerName). The problem of having the property expressed as string is that the OrderBy method accepts an Expression.
Adam Anderson's Blog builds on the example Nick provided. He has a wonderful example of an OrderByHelper that can translate the jtSorting string into an Expression. Using the OrderByHelper, you can extend the OrderBy method to accept jtSorting as an input. Copy the code from his page into a new class that includes System.Linq.Expressions and System.Reflection. Once the OrderyHelper is part of your project, you can begin to pass jtSorting as an OrderBy parameter. Your code will then simplify into...
List<Wine> wines = db.Wines.OrderBy(jtSorting).Where(w => w.Active == true).Where(w => w.ProducerID == ProducerID).ToList();
Hope this helps!
Upvotes: 0
Reputation: 7527
1: i think even pass a Boolean is better than pass a string to indicate orderby, like
bool isDesc
2:
var wines = db.Wines.Where(w => w.Active && w.ProducerID == producerID);
if(isDesc)
{
wines = wines.OrderByDesc(w=>w.XXX);
}
else
{
wines = wines.OrderBy(w=>w.XXX);
}
var list = wines.ToList();
3: Is that better to use AutoMapper to map your object from one type to another, unless you really want to do anonymous.
Upvotes: 1
Reputation: 4841
You want dynamic sorting, where you pass a field and an order?
Dynamic LINQ OrderBy on IEnumerable<T>
Upvotes: 1