Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove an item from a LINQ collection

I think I am putting my question correctly :)

I am querying the database using LINQ to retrieve some data. Please find the code below.

var leadtasktype = _context.LeadTypeTaskTypes.Where(l => l.LeadTypeId == item.Value);

foreach(LeadTypeTaskType l in leadtasktype){
    if (l.TaskTypeId == 21)
    {
        //I need to remove an item which has the tasktype id 21
    }
}

As I mentioned in the comments, I need to remove items from the leadtasktype based on my if condition. How do I do this?

like image 580
Joshua Avatar asked Dec 04 '22 01:12

Joshua


2 Answers

This would seem very like another filter condition:

var leadtasktype = _context.LeadTypeTaskTypes
    .Where(l => l.LeadTypeId == item.Value)
    .Where(l => l.TaskTypeId != 21); // Exclude ID = 21.
like image 84
Paul Turner Avatar answered Dec 20 '22 00:12

Paul Turner


var leadtasktype = _context.LeadTypeTaskTypes.Where(l => l.LeadTypeId == item.Value && l.LeadTypeId != 21);
like image 41
Alex Kovanev Avatar answered Dec 20 '22 00:12

Alex Kovanev