Removing items in List using LINQ [duplicate]

This question already has an answer here:

I have a List of type some entity

List

public class OrderLine
{
   public string productCode;
   public int quantity;


}

i need to remove items from the above List if the productCode is equal to some products.

List ProductsToBeExcluded = new List(){"1234","1237"};

so, from List i need to remove products which are equal to 1234 and 1237

i have tried

  1. create a List from List using

      List OrderLines = GetOrderLines();
      var ol = from o in OrderLines
            select o.ProductCode;
    

2.

  List ProductsToBeExcluded = new List(){"1234","1237"};
   var filtered = OrderLines.Except(ProductsToBeExcluded);

how do I proceed further in removing

thanks

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?

Remove Item have same key in List C# [duplicate]

This question already has an answer here:

I have a List n Objects.
In n Objects have some object that they have same Id.
Example List = {t1, t2, t3, t4, t5....,t10}.

assuming that:

t1.Id = t5.id

So i want to remove t5 from that List.

Who can help me plz.

In this question: using LINQ to remove objects within a List you know that object duplicates (ex: “bob”), but in my question, in this List, i don’t know any object duplicate before.

Using LINQ to remove elements from a List

Say that I have LINQ query such as:

var authors = from x in authorsList
              where x.firstname == "Bob"
              select x;

Given that authorsList is of type List, how can I delete the Author elements from authorsList that are returned by the query into authors?

Or, put another way, how can I delete all of the firstname’s equalling Bob from authorsList?

Note: This is a simplified example for the purposes of the question.

Using LINQ to delete an element from a ObservableCollection Source [duplicate]

Actually, it is a closer duplicate of:
RemoveAll for ObservableCollections?

Possible Duplicate:
using LINQ to remove objects within a List

This is the code I’m using, but its not very readable. Can I use LINQ to shorten the code below while still having the same functionality?

int index = 0;
int pos = 0;

foreach (var x in HomeViewModel.RecentPatients)
{
   if (x.PID == p.PID)
       pos = index;
   else
       index++;

}

HomeViewModel.RecentPatients.RemoveAt(pos);