开发者

issue while getting values from Dictionary<int, List<int>>

I am having a dictionary which was defined as follows

Dictionary<int, List<int>> dict1 = new Dictionary<int, List<int>>();

I will have a list element where i can store the values which will come from database

List<int> lstdbTaskID = new List<int>();

assume this holds

100 105 110 200

respectively.

I will have my dictionary with values stored as follows. Assume that i have 2 keys 10,20 respectively, and for this key values i will have my values as for 10 i will have 100,105 and 110 and for 20 i will have 200.

I would like to compare these values with the list available lstdbTaskID

I tried this

foreach (int strlst in lstdbTaskID)
{
    if (dict1.ContainsValue(lstdbTaskID[strlst]))
}

But i am getting errors as follows

The best overloaded method match for开发者_如何学运维 'System.Collections.Generic.Dictionary>.ContainsValue(System.Collections.Generic.List)' has some invalid arguments`

and

Cannot convert from 'int' to 'System.Collections.Generic.List'`

can any help me on this?


Your code is wrong at because you are trying to compare int value with List of int.

Your dictionary is: Dictionary of int to List of int.

and you have another structure as List of int

so when you do:

// Compiler fails here because you are trying to check whether dictionary contains
// the given integer value. Dictionary in this case has a list of integers as its `Value` 
// in its `<Key,Value>` pair.
dict1.ContainsValue(lstdbTaskID[strlst])

Use linq statement:

foreach (int strlst in lstdbTaskID)
{
    if (dict1.Any(pair => pair.Value.Contains(strlst)))
    {
        // do something
    }
}

Edit: If you want this without linq, do the linq task by self.

foreach (int strlst in lstdbTaskID)
{
    foreach (int key in dict1.Keys)
    {
        if (dict1[key].Contains(strlst))
        {
            // do something
        }
    }
}


Look at the type of the value you're storing in your dictionary - it's a List<int>, not an int. So it makes no sense to ask whether the dictionary contains a value of 5, say.

However, it does make sense to ask if the dictionary contains any value (list) which itself contains 5.

For example:

foreach (int strlst in lstdbTaskID)
{
    if (dict1.Values.Any(list => list.Contains(strlst))
    {
        ...
    }
}

However, that's really not a very efficient way of representing it. It's not clear what the best of representing it is though, without knowing what you're going to do with the results. You may just want a HashSet<int> containing all the values from all the lists, for example - but if you want to get back to the keys whose values contained a particular ID, that's a different matter.

EDIT: In .NET 2.0 land, you could use:

foreach (int strlst in lstdbTaskID)
{
    foreach (List<int> list in dict1.Values)
    {
        if (list.Contains(strlst))
    }
}

... but you're really not using the dictionary as a dictionary in either case here...


I am having slight problems understanding your question fully, however, my answer should push you in the right direction. Seeing as you do not have access to Linq (as you are using .Net 2.0):

static bool IsContained(IEnumerable<int> lstdbTaskID, Dictionary<int, HashSet<int>> dict1)
{
    foreach (int strlst in lstdbTaskID)
        foreach (HashSet<int> value in dict1.Values)
            if (value != null && value.Contains(strlst))
                return true;
    return false;
}

You should use a HashSet<int> as it is far faster for looking up values (and is supported in .Net 2.0); however, you should not use HashSet<int> (and instead use List<int>) if:

  • The list needs to store duplicates.
  • - or - The order of the values is important.


Well, dict1 is a dictionary of dictionaries, not ints. So dict1.ContainsValue takes a dictionary as a parameter - you're checking whether it contains a given dictionary or not. But you're giving it an int.

Try this:

if (dict1.Any(x => x.Value.ContainsValue(strlst))) // ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜