Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

double value = (from kv in SortedList
                where kv.Key.Code = "CodeI'mLookingFor"
                select kv.Value.Value).FirstOrDefault();

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top