I really do believe that I’m not asking a good question, but I’m new newbie
When I’m programming for example I try to use the contain method for finding a value in the dictionary data type:
I cud do it in my mind by two way:

dict.Values.Contains(str)(however I had an error using it)

or

dict.ContainsValue(str)

i'm confused
thank you

Edited 6 Years Ago by zizi: n/a

Each of those methods is going to return a boolean if the value is contained within the dictionary. You appear to be using a string variable to as the test, so it assumes the TValue type of the dictionary is a string. You would be using the methods in the following manner:

Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "one");
dict.Add(2, "two");

bool contains1 = dict.Values.Contains("one");
bool contains2 = dict.ContainsValue("two");

A note about the methods: ContainsValue is an instance method of the dictionary class, whereas Values.Contains is an extension method belonging to IEnumerable<T> and is part of Linq.

thanks apegram,

TValue type of the dictionary is a string

yes thats the problem I noticed that the Tvalue is a list of string,

Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();

so how am i going to directly get to the contains of that list with these code lines?I cudn't figer it out,I think we need another list!?

Contains is an extension method belonging to IEnumerable<T> and is part of Linq.

can you explain this more.

regards:)

Edited 6 Years Ago by zizi: n/a

If you are trying to find if your dictionary contains a list that contains a given string, you could do it like this using LINQ.

bool contains = dictionary.Values.Any(list => list.Contains("a"));

If you want to retrieve the actual lists that contain the value, then you could do so with the following code samples. I'm showing two versions: a query expression syntax version and a lambda version.

var query = from list in dictionary.Values
            where list.Contains("a")
            select list;

var lambdaQuery = dictionary.Values.Where(list => list.Contains("a"));

thanks apegram,
can you explain this more.

LINQ and extension methods are too much to explain in a single post. The short short version is that LINQ stands for language integrated query and it makes querying collections much easier while using familiar syntax if you are from a SQL background (if you choose to use query expression syntax). Extension methods are methods that are created that appear to add methods to classes but are really static methods in unrelated static classes that use a special parameter syntax that allows you to use the method on an instance of a given class. Example:

static class Foo
{
    public static int MultiplyBy(this int x, int y)
    {
        return x * y;
    }
}

...

int a = 14;
int b = a.MultiplyBy(2);

Edited 6 Years Ago by apegram: n/a

Comments
Nice job - you are clearly an expert with these things
This question has already been answered. Start a new discussion instead.