In this demo we show how you can have a count property in the grid, while also being able to sort and group by that property.
For this to work you need to add a get only property to your Entity (Lunch in our case public int MealsCount => Meals.Count();),
after this you can use the count property as any other property.
However if you're using EntityFramework you'll also need to set the OrderByFunc on the GridModelBuilder.
public class GridWithListCountColumnController : Controller { public IActionResult Index() { return View(); }
public IActionResult GetItems(GridParams g) { var query = Db.Lunches.AsQueryable();
var gmb = new GridModelBuilder<Lunch>(query, g) { KeyProp = o => o.Id,// needed for Entity Framework | nesting | tree | api Map = o => new { o.Id, o.Person, FoodName = o.Food.Name, FoodPic = o.Food.Pic, o.Location, o.Price, o.MealsCount, MealsStr = string.Join(", ", o.Meals.Select(m => m.Name)) }, OrderByFunc = (lunches, rules) => { lunches = Dlinq.OrderBy(lunches, rules, new Dictionary<string, LambdaExpression> { { "MealsCount", ExprHelper.GetCountLambda<Lunch>(o => o.Meals) } });
return lunches; } };
return Json(gmb.Build());
// setting OrderByFunc in this demo is actually unnecessary because we're not using Entity Framework // and we have Lunch.MealCount property that returns the count, // but EF won't be able to translate OrderBy MealCount into a sql query } }
Comments
By accessing this site, you agree to store cookies on your device and disclose information in accordance with our cookie policy and privacy policy.