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