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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:44:00+00:00 2026-05-18T03:44:00+00:00

A simplified scenario: I have a List<Foo> . Foo has two properties Description (

  • 0

A simplified scenario:

  • I have a List<Foo>.
  • Foo has two properties Description (string), IsFoo (bool)

E.g:

var foos = new List<Foo>();

User can “add new Foo’s” via textboxes, then on form submit i do this:

foos.Add(new Foo { Description = txtOne.Text, IsFoo = true });
foos.SaveToDb();

However, there are multiple textboxes, and if for example they type “FooBar” in textbox one, then “FooBar” in textbox two, i do not want to show an error, but i simply do not want to add them to the collection. (don’t worry about the reason behind this, this is a simplified scenario).

I don’t need to show anything to the UI, just when they submit the form, before persisting to the database i need to remove any duplicates (or prevent them from being added to the list in the first place).

What’s the easiest/best way to do this? Dictionary perhaps?

I’m using C#4, LINQ, .NET 4.

  • 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-18T03:44:00+00:00Added an answer on May 18, 2026 at 3:44 am

    You can use a HashSet<Foo>.

    HashSets are unique, unordered collections.
    Adding an element that already exists will silently do nothing. (and return false)

    Note that you must override Equals and GetHashCode on the Foo class to compare by value.

    Also note that hashsets are intrinsically unordered; if you care about insertion order, you can’t use it.


    Alternatively, you can use LINQ to check whether your list has a duplicate:

    if (!foos.Any(f => f.Description == txtOne.Text))
        foos.Add(new Foo { Description = txtOne.Text, IsFoo = true });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a simplified scenario to cut to the chase: I have one webpage
My scenario is this: I have a table with a structure like this (simplified)
I have a scenario where a class loads objects of one type, due do
This is a more theoretical question, not a specific scenario: Let's assume, we have
I'm running rails 3.0, rspec 2.0 and factory_girl. Here is the simplified scenario I'm
I'm using Sybase 12.5.3 (ASE) ; I'm new to Sybase though I've worked with
Is it possible to bind data in the wrong direction? I want a value
I was coding this up in C# and the quickest solution to come to
I am working on a piece of software which generated a polygon mesh 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.