Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value from C# lookup

I'm interested in how to get value from C# lookup structure.

Example:

var myLookup = (Lookup<string, int>)data.Rows.Cast<DataRow>().ToLookup(row => row["Name"], row => row["Id"]);

foreach (var myLookupItem in myLookup)
                    {
                        Debug.WriteLine("Name: " + myLookupItem.Key);
                        Debug.WriteLine("Id: " + myLookupItem.ToString());
                    }

Problem is that the

myLookupItem.ToString()

doesn't display actual value, instead only System.Linq.Lookup2[System.String,System.Int32] is displayed.

Later on, I should get the lookup value using lambda:

 int lookupValue = myLookup.Where(x => x.Key == "Test").Select(x => x).FirstOrDefault());

but this also gives the same as above.

Please advise how to achieve this.

Thanks in advance.

like image 524
lelol Avatar asked Dec 13 '11 12:12

lelol


People also ask

How does C return values?

In this program, no arguments are passed to the function “sum”. But, values are returned from this function to main function. Values of the variable a and b are summed up in the function “sum” and the sum of these value is returned to the main function.

Can I return 2 values from a function in C?

In C or C++, we cannot return multiple values from a function directly.

What does %d do?

%d takes integer value as signed decimal integer i.e. it takes negative values along with positive values but values should be in decimal otherwise it will print garbage value. ( Note: if input is in octal format like:012 then %d will ignore 0 and take input as 12) Consider a following example.

What does %d do in C?

%d is a format specifier, used in C Language. Now a format specifier is indicated by a % (percentage symbol) before the letter describing it. In simple words, a format specifier tells us the type of data to store and print. Now, %d represents the signed decimal integer.


1 Answers

That's because the lookup item is a collection. You can see every value of the lookup like this:

foreach (var myLookupItem in myLookup)
{
    Debug.WriteLine("Key: " + myLookupItem.Key);
    foreach (var myLookupValue in myLookupItem)
    {
        Debug.WriteLine("Value: " + myLookupValue);
    }
}
like image 131
miguel.hpsgaming Avatar answered Sep 19 '22 05:09

miguel.hpsgaming