I have List of Jo Cards, i want to remove Job Cards where the VehicleID
count
is greater than two
Here is My Attempt.
var OpenJobCards = await _context.WorkshopJobCards.Include(wjc => wjc.WorkshopJobCardCategory).Where(wjc => wjc.Job_Card_Closed == false).ToListAsync() ;
OpenJobCards.Remove(OpenJobCards.GroupBy(wjc => wjc.VehicleID).Count() >2);
as far as I understand you are using DBContext. If it is so, please also keep in mind that it is better to use Queriable where possible so that filtering is done on the DB side and does not fetched into your application memory
var listToRemove = await _context.WorkshopJobCards.Include(wjc => wjc.WorkshopJobCardCategory)
.Where(wjc => wjc.Job_Card_Closed == false).GroupBy(wjc => wjc.VehicleID)
.Where(t => t.Count() >2)
.Select(x => new OpenJobCard() {Id = x.Key});
_context.entity.RemoveRange(listToRemove);
Based on your comment, it would seem you're actually looking at this the wrong way. If vehicles have a collection of job cards, and your ultimate goal is to show only vehicles with less than 2 job cards assigned to them, then just do:
var vehicles = await _context.Vehicles.Where(x => x.JobCards.Count() < 2).ToListAsync();
Done.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.