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

  • SEARCH
  • Home
  • 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

Related: How to add new items to right-click event on Folders and Files in
Related to this question , I decided to check the UDFs in my data
Related to this question: URL characters replacement in JSP with UrlRewrite I want to
Related to this question , what is the best practice for naming a mutex?
This question is related to this one , though I think I was a
Much related to this question , we have a scenario on my team where
This query is related to this one I asked yesterday. I have a radio
[This question is somewhat related to this question , but the answers are not...]
This is related to this question . I'm writing a Flex app (a WindowedApplication)
This question is related to this other question I recently asked... cf.net exception and

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.