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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:56:39+00:00 2026-05-18T19:56:39+00:00

I am learning programming and my problem is that I have a bunch of

  • 0

I am learning programming and my problem is that I have a bunch of objects and I want to add these objects to a list only if the list does not already contain that object. Secondly if the object is already contained I want to ignore that object and add the next one instead. I think I have got the first part working just need some help with part two. Many thanks.

PartyGroup partyGroup = new PartyGroup(); 

using (AseDataReader reader = command.ExecuteReader()) 
{ 
    while (reader.Read()) 
    {  
        if (!myPartyGroupList.Contains(partyGroup)) 
        { 
            partyGroup.PartyGroupID = Convert.ToInt32(reader["party_group_id"]); 
            partyGroup.PartyGroupName = reader["party_group_name"].ToString(); 
            partyGroup.PersonList = myPersonList; 

            myPartyGroupList.Add(partyGroup); 
        } 
        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-18T19:56:39+00:00Added an answer on May 18, 2026 at 7:56 pm

    When comparing it is better to use comparison with respect to a identifier, which in your case is the PartyGroupId. If you use contains then the default overload of Contains() then the hash value of the object in the list is used for comparison.

    So rather than leave the comparison to .NET you can create a custom IEqualityComparer implementation or use Linq’s Where clause as follows.

    
    using (AseDataReader reader = command.ExecuteReader()) 
    { 
        while (reader.Read()) 
        {  
            int groupId = Convert.ToInt32(reader["party_group_id"]);
    
            if (partyGroupsList.Where(partyGroup => partyGroup.PartyGroupID == groupId).Any() == false)
            {
               PartyGroup newPartyGroup = new PartyGroup()
                                          {
                                              PartyGroupID = groupId,
                                              PartyGroupName = reader["party_group_name"].ToString(),
                                              PersonList = myPersonList
                                          };
    
               partyGroupsList.Add(newPartyGroup);                 
            } 
    
            // If object already exists in the list then do not add, continue 
            // to the next row.   
        } 
    } 
    

    Another suggestion is to rename to PartyGroup class members as:

    
    class PartyGroup
     {
       public int ID { get; set; }
       public string Name { get; set; }
       public IList PersonList { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning programming and my problem is that I have a bunch of
I'm currently learning the network programming with C/C++... I have experience .NET but not
I have come to learn that my learning habits in programming is by looking
I'm learning functional programming, and have tried to solve a couple problems in a
I'm learning OCaml and although I have years of experience with imperative programming languages
I'm a real noob who just started learning 3d programming and i have a
I'm sketching a design of something (machine learning of functions) that will preferably want
I'm learning programming with Java. This is my solution to the second problem in
I'm learning iPhone programming from Erica Sadun's The iPhone Developer's Cookbook. When I run
As I continue my quest of learning functional programming, I've come to wonder if

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.