RPM1984
RPM1984

Reputation: 73112

How to maintain unique List Before Saving To Database? - C#

A simplified scenario:

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.

Upvotes: 1

Views: 452

Answers (3)

cdhowie
cdhowie

Reputation: 168988

To expand on SLaks' answer, you could do something like this:

public class FooComparer : IEqualityComparer<Foo> {
    public static readonly FooComparer Instance = new FooComparer();

    private FooComparer() { }

    public bool Equals(Foo a, Foo b) {
        if (a == null)
            return b == null;

        if (b == null)
            return false;

        // For case-sensitivity:
        return a.Description == b.Description;

        // For case-insensitivity:
        return String.Equals(a.Description, b.Description, StringComparison.OrdinalIgnoreCase);
    }

    public int GetHashCode(Foo obj) {
        // For case-sensitivity:
        return obj.Description.GetHashCode();

        // For case-insensitivity:
        return StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Description);
    }
}

Then store your items in a HashSet<Foo> like so:

var hashSet = new HashSet<Foo>(FooComparer.Instance);
hashSet.Add(new Foo() { ... });

With this code, if a second Foo object is added to the hashset and has an identical description as one already present in the hashset, the new object will simply not be added.

Upvotes: 2

Mr Shoubs
Mr Shoubs

Reputation: 15389

Can you use Distinct in linq?

This is VB (and not accurate as I've not got VS on this machine), but something along the lines of:

Dim ie as IEnumerable(of Foo) = From obj As Foo In Foo's Select obj Distinct

Then implent IEqualityComparer? - lookd like @cdhowie just answered....

Upvotes: 0

SLaks
SLaks

Reputation: 887305

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 });

Upvotes: 3

Related Questions