Alex
Alex

Reputation: 3998

Generic Dictionary retrieve value

I have the following C# class:

class BatchData
{
    public string batchNumber { get; set; }
    public string processDate { get; set; }
    public int TotalRecords { get; set; }
    public int SuccessCount { get; set; }
}

and a dictionary:

Dictionary<int, BatchData> BatchData = new Dictionary<int, BatchData>();

Now, I want to search the whole dictionary to see if the value

x

is held in:

BatchData.batchNumber 

eg

for the whole dictionary, if

BatchData.batchNumber = x

I know Dictionary has a method

.contains

But I am not sure how I can apply this.

EDIT:

BatchData.batchNumber = x

Can occur multiple times within the dictionary

Upvotes: 3

Views: 101

Answers (4)

isxaker
isxaker

Reputation: 9506

You can use another one solution by Contains method from System.Linq.

First, you need to implement IEqualityComparer<> interface

public class BatchDataComparer : IEqualityComparer<KeyValuePair<int, BatchData>>
{
    public bool Equals(KeyValuePair<int, BatchData> x, KeyValuePair<int, BatchData> y)
    {
        return (x.Value.batchNumber == y.Value.batchNumber);
    }

    public int GetHashCode(KeyValuePair<int, BatchData> obj)
    {
        //or something else what you need
        return obj.Value.batchNumber.GetHashCode();
    }
}

After that, you can get value from Dictionary like this:

private static void Main(string[] args)
{
    Dictionary<int, BatchData> dic = new Dictionary<int, BatchData>();

    dic.Add(1, new BatchData() { batchNumber = "x" });
    dic.Add(2, new BatchData() { batchNumber = "y" });
    dic.Add(3, new BatchData() { batchNumber = "z" });

    bool contain = dic.Contains(new KeyValuePair<int, BatchData>(100, new BatchData()
    {
        batchNumber = "z"
    }), new BatchDataComparer());

    Console.ReadKey();
}

public class BatchData
{
    public string batchNumber { get; set; }
    public string processDate { get; set; }
    public int TotalRecords { get; set; }
    public int SuccessCount { get; set; }
}

Upvotes: 0

dana
dana

Reputation: 18155

You should use batchNumber as the key to your dictionary:

Dictionary<string, BatchData> BatchData = new Dictionary<string, BatchData>();
BatchValues.Add(batch1.batchNumber, batch1);
BatchValues.Add(batch2.batchNumber, batch2);
BatchValues.Add(batch3.batchNumber, batch3);
...

Then checking for existence is an O(1) operation (link):

BatchValues.ContainsKey(batchNumber);

Upvotes: 1

D Stanley
D Stanley

Reputation: 152644

A dictionary is a collection of KeyValuePair objects, each of which has a Key property (an int in your case), and a Value property (a BatchData object).

There could be multiple entries with that batch number. If you just want to see if any key contains that number you can use

batchData.Any(kvp => kvp.Value.batchNumber == x);

If you want all key-value pairs with that batch number, change to Where:

batchData.Where(kvp => kvp.Value.batchNumber == x);

You can also use First, Single, etc. as appropriate.

Upvotes: 3

Ric
Ric

Reputation: 13248

You could do this:

BatchData.Values.Any(x=>x.batchNumber == "x");

For example:

Dictionary<int, BatchData> BatchData = new Dictionary<int, BatchData>();
BatchData.Add(1, new BatchData { batchNumber = "x"});
var hasX = BatchData.Values.Any(x=>x.batchNumber == "x"); //true;

Upvotes: 5

Related Questions