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 774853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:11:51+00:00 2026-05-14T19:11:51+00:00

This is similar to my last question; but from a different angle. See if

  • 0

This is similar to my last question; but from a different angle.
See if item exists once in Enumerable (Linq)

Given the following set of items, and lists containing them…

Item 1
Item 2
Item 3
Item 4
Item 5

class Item
{
 string Name { get; set; }
}

List<Item> available = new List<Item>()
{
 Item 1
 Item 1
 Item 2
 Item 3
 Item 5
}

List<Item> selected = new List<Item>()
{
 Item 1
 Item 2
 Item 3
}

I need to make a third List that has everything from “available”, except what is in “selected”.
However ‘Item 1’ is in ‘available’ twice, but only in ‘selected’ once. Since they are instances of the same item, I am having trouble figuring out the appropriate logic to accomodate this.

The final array should look like…

List<Item> selectable = new List<Item>()
{
 Item 1
 Item5
}
  • 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-14T19:11:52+00:00Added an answer on May 14, 2026 at 7:11 pm

    There may be a LINQ method to accomplish this task. You could certainly get the 5 item since it is unique, but the second item 1 could prove difficult. However, you could always do this the old fashioned way and create the new list yourself. Consider this example:

    class Item
    {
        public Item(string name) { Name = name; }
        public string Name { get; set; }
    }
    

    …

    List<Item> available = new List<Item>()
    {
        new Item("1"), new Item("1"), new Item("2"), new Item("3"), new Item("5")
    };
    
    List<Item> selected = new List<Item>()
    {
        new Item("1"),new Item("2"), new Item("3")
    };
    
    List<Item> stillAvailable = new List<Item>();
    List<Item> stillSelected = new List<Item>(selected);
    
    foreach (Item item in available)
    {
        Item temp = stillSelected.Find(i => i.Name == item.Name);
        if (temp == null)
            stillAvailable.Add(item);
        else 
            stillSelected.Remove(temp);
    }
    

    You create a list for the items still available, which is initially empty. You create a list for the items still selected, which contains all of the selected items. You then simply iterate over the available items and search the stillSelected list. If the item is found, you remove it from the stillSelected list. If not, you add it to the stillAvailable list. By the end of the loop, stillAvailable will contain a single item 1 and item 5.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is similar to (but different from) this question . Here is some simple
Different from this question , but similar in that I don't get an error
Okay this is similar to my last question but what I ended up doing
This is similar to this question I asked last week. My dataGrid is populated
I have checked this similar question, but the suggestions did not solve my problem:
I found this similar question here , but this is really old. Was it
(this is similar to GNU make: Execute target but take dependency from file but
First off, fairly new to JS but getting better :-) This question is similar
I was asking the similar question last saturday, but seems like the smart folks
This question is similar to my last question . Except this time I'm using

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.