Checking a unique item in a list (C #)

I have a list of items. An element is an object with two fields. One field is of type Dictionary<string, List<string>, and the second is of type int. Now I want to check if there is an element with both fields unique in this list. In the dict field, I care about the key part, not the value, so the part of the value can be the same between the elements. This is a key that must be unique on this list.

If such an element exists, I want to be able to get the position of this element in my list.

Hope this is understandable.

To clarify -

Here is my class

namespace Polstyr
{
class RecordItem
{
    Dictionary<string, List<string>> dict;

    public string MachineNr { get; set; }

    public RecordItem()
    {
        dict = new Dictionary<string, List<string>>();
    }

    public void AddToDict(string value, List<string> list)
    {
        dict.Add(value, list);
    }

    public Dictionary<string, List<string>> GetDictionary
    {
        get
        {
            return dict;
        }
    }
}

}

In another part of my code, I have a list of type List<RecordItem>called recordItems.

, RecordItem recordItems, . dict , MachineNr .

.

+3
3

LINQ, , , .

:

class A
{
    // your int field
    public int Bar{get; set;} 

    // your dictionary (the value is irrelevant so I've just used bool)
    public Dictionary<string, bool> Foo{ get; set; } 
}

:

var nonUniqueFoo = list.SelectMany(a => a.Foo.Keys)
                    .GroupBy( s => s)
                    .Where( g => g.Count() > 1)
                    .Select(g => g.Key);


var nonUniqueBar = list.Select(a => a.Bar)
                    .GroupBy( i => i)
                    .Where( g => g.Count() > 1)
                    .Select(g => g.Key);

var uniqueObjects = list.Where( a=> !nonUniqueBar.Contains(a.Bar) )
                       .Where( a => !nonUniqueFoo.Intersect(a.Foo.Keys).Any() )
              ;

, :

List<A> list = new List<A>();
list.Add( new A{ Bar=1, Foo = new Dictionary<string, bool>{ {"123", true} } } );
list.Add( new A{ Bar=2, Foo = new Dictionary<string, bool>{ {"456", true}, {"789", true}, {"567", true} } } );
list.Add( new A{ Bar=3, Foo = new Dictionary<string, bool>{ {"AAA", true}, {"456", true}, {"567", true} } } );
+3

GetHashCode IEquatable

public class RecordItem:IEquatable<RecordItem>
{
...

    public override int GetHashCode()
    {
        int i=0;
        if (dict != null)
        {
            foreach (KeyValuePair<string, List<string>> pair in dict)
                i += pair.Key.GetHashCode();
        }
        i += MachineNr.GetHashCode();
        return i;
    }

    public bool Equals(RecordItem item)
    {

        if (MachineNr != item.MachineNr)
            return false;
        else
        {
            if ((dict != null && item.dict == null) || (dict == null && item.dict != null))
                return false;
            else if (dict != null && item.dict != null)
            {
                foreach (KeyValuePair<string, List<string>> pair in dict)
                {
                    if (!item.dict.ContainsKey(pair.Key))
                        return false;
                }
                return true;
            }
            else return true;
        }
    }
}

GetHashCode - MachineNr. , 2 .
Contains .

RecordItem i1 = new RecordItem{ MachineNr="M1"};
i1.AddToDict("1", new List<string>{ "A","B"});
i1.AddToDict("2", null);

RecordItem i2 = new RecordItem{MachineNr = "M1"};
i2.AddToDict("1", null);
i2.AddToDict("2", new List<string> { "A", "B" });

List<RecordItem> lstItem = new List<RecordItem>();
lstItem.Add(i1);
Console.WriteLine(lstItem.Contains(i2));

+2

. , .

, 1.

, :

class Main
{
    void DoIt()
    {
        Thing[] collection = new [] { new Thing(), new Thing() };
        var lookupTable = new Dictionary<KeyValuePair<int, string>, List<int>>();
        int index = 0;
        foreach (Thing item in collection)
        {
            KeyValuePair<int, string> key = new KeyValuePair<int, string>(item.Bar, item.Foo.Key);
            if (!lookupTable.ContainsKey(key))
                lookupTable.Add(key, new List<int>());
            lookupTable[key].Add(index++);
        }
    }
}

class Thing
{
    public KeyValuePair<string, List<string>> Foo { get; set; }
    public int Bar { get; set; }
}

EDIT: Now, I saw your code, I think it will be very confusing. I mistakenly assumed that your recording element has a property:

KeyValuePair<string, List<string>>

but not:

Dictionary<string, List<string>>

I no longer understand what position (indices) you need to get. I'm afraid you have to clarify. In particular, you say:

The key in the dict must be unique

but there are many keys in the dictionary. Do you mean that key collection must be unique?

+1
source

Source: https://habr.com/ru/post/1725857/


All Articles