views:

282

answers:

3

Hi

I'm a complete begginer with Linq. And I woild like to know, if it is possible to make a query where for a given Class1.Code I get matching Class2.Value.

class Class1()
{
    public string Code;
    ...
}

class Class2()
{
    public double Value;
    ...
}

SortedList<Class1, Class2>

Thank you for your help.

+1  A: 
double value = (from kv in SortedList
                where kv.Key.Code = "CodeI'mLookingFor"
                select kv.Value.Value).FirstOrDefault();
LorenVS
+2  A: 
list.First(x => x.Key.Code == codeToSearch).Value

However, this is not efficient (O(n)). I guess this is not the correct way to approach the problem. If you are searching by Code most of the time, you should probably make it a SortedList<string, Class2> and store Code as the key.

Mehrdad Afshari
A: 

SortedList < Class1, Class2 > x;

One way to write it:

(from pair in x where pair.Key.Code == matchingValue select pair.Value.Value)

where matchingValue is the Class1.Code you want to search for.

Dan Cristoloveanu