I have string column contains 1st Batch, 2nd Batch, 10th Batch etc. Now I am trying to sort them using this string field on ascending order e.g. 1, 2, 3 but having no luck.
public ActionResult PublicGrid()
{
ViewBag.members = db.Members.Count().ToString();
return View(db.Members.OrderBy(a => Regex.Replace(a.Batch, @"[^\d]", String.Empty)).ThenBy(a=> a.LastName).ToList());
}
I have tried the above reg expression but getting error -
LINQ to Entities does not recognize the method 'System.String Replace(System.String, System.String, System.String)' method, and this method cannot be translated into a store expression.
I have tried this Linq expression but still, throws error -
var members = (from m in db.Members
orderby Char.IsDigit(Convert.ToChar(m.Batch)) ascending
select m).ToList();
I have tried creating a static function to split the string like below but still not working.
public ActionResult PublicGrid()
{
ViewBag.members = db.Members.Count().ToString();
return View(db.Members.OrderBy(a => NumberOnly((a.Batch))).ThenBy(a=> a.LastName).ToList());
}
public static string NumberOnly(string strRaw)
{
string numbers = string.Empty;
foreach (char c in strRaw)
{
if (Char.IsNumber(c))
{
numbers += c;
}
}
return numbers;
}
Any help much appreciated. Thanks in advance.
Someone mentioned this question is similar to previously asked questions but I could not find any similarity.