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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:02:55+00:00 2026-05-16T04:02:55+00:00

I have a list List<MyObject> myList and I am adding items to a list

  • 0

I have a list

  List<MyObject> myList

and I am adding items to a list and I want to check if that object is already in the list.

so before I do this:

 myList.Add(nextObject);

I want to see if nextObject is already in the list.

The object “MyObject” has a number of properties but comparison is based on matching on two properties.

What is the best way to do a check before I add a new “MyObject” to this list of “MyObject”s.

The only solution I thought up was to change from a list to a dictionary and then make the key a concatenated string of the properties (this seems a little unelegant).

Any other cleaner solutions using list or LINQ or something else?

  • 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-16T04:02:56+00:00Added an answer on May 16, 2026 at 4:02 am

    It depends on the needs of the specific situation. For example, the dictionary approach would be quite good assuming:

    1. The list is relatively stable (not a lot of inserts/deletions, which dictionaries are not optimized for)
    2. The list is quite large (otherwise the overhead of the dictionary is pointless).

    If the above are not true for your situation, just use the method Any():

    Item wonderIfItsPresent = ...
    bool containsItem = myList.Any(item => item.UniqueProperty == wonderIfItsPresent.UniqueProperty);
    

    This will enumerate through the list until it finds a match, or until it reaches the end.

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

Sidebar

Related Questions

I have this class: public class MySerializableClass { public List<MyObject> MyList { get; set;
I have a List<MyObject> that I retrieve from the database. However, I would like
I have a List of MyObjects ... MyObject{ int id,String name}. Now I want
I have a list like this: List<Controls> list = new List<Controls> How to handle
I have several templated objects that all implement the same interface: I.E. MyObject<datatype1> obj1;
Say I have a List of objects, and the object has a string property.
I have a list that gets refreshed every 2 seconds via the Handler postDelayed()
I have a strongly typed list of custom objects, MyObject , which has a
I have an object which is contained within a List<>, I need to remove
I have found this example on StackOverflow: var people = new List<Person> { new

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.