Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1083669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:27:56+00:00 2026-05-16T22:27:56+00:00

Related to this: Adding new items dynamically to IQueryable hard-coded fake repository How could

  • 0

Related to this: Adding new items dynamically to IQueryable hard-coded fake repository

How could I build the method for the following class, which would remove items from the list based on the value of one of its fields?

public class FakeProductsRepository 
{
  private readonly List<Product> fakeProducts = new List<Product>
  {
      new Product { ProductID = "xxx", Description = "xxx", Price = 1000 },
      new Product { ProductID = "yyy", Description = "xxx", Price = 2000 },
      new Product { ProductID = "zzz", Description = "xxx", Price = 3000 },
  };

  public void AddProduct(string productID, string description, int price)
  {
      fakeProducts.Add(new Product
      {
          ProductID = productID,
          Description = description,
          Price = price,
      });
  }

  public void RemoveProduct(string productID)
  {
      ????????
      //How to remove the item from the fakeProducts List where ProductID == productID?
  }

  public IQueryable<Product> Products
  {
      get { return fakeProducts.AsQueryable(); }
  }
}

The problem method is pointed out with “???????” and the comment string.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:27:56+00:00Added an answer on May 16, 2026 at 10:27 pm

    In general, for a collection I’d use this code:

    var productsToRemove = fakeProducts.Where(p => p.ProductID == productID).ToList();
    foreach(var product in productsToRemove)
    {
       fakeProducts.Remove(product);
    }
    

    Don’t forget the ToList(), or you can get an InvalidOperationException saying “Collection was modified”.

    Update (thanks to linuxuser27):
    But List<T> also has a special method, taking Predicate<T>:

    fakeProducts.RemoveAll(product => product.ProductID == productID);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is related to following thread. Prism RegionAdapter - Removing then Adding View
I'm having a memory problem related to UIImageView. After adding this view to my
This is pretty closely related to another SO question. Using the example below, could
Related: How to add new items to right-click event on Folders and Files in
Related to this question , is the idea of a default servlet that serves
Note: this question is related to this one , but two years is a
This is related to this question I asked earlier about syntax highlighting user-defined blocks
Somewhat related to this question , but in the absence of any answer about
I found this related question: How do I use composition with inheritance? I would
This question is related to this , hopefully better phrased. I would like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.