We help IT Professionals succeed at work.

How do I find an entry in a List<KeyValuePair<,>> using Linq?

jxbma
jxbma asked
on
Hi:

I'm just starting to get my feet wet with Linq.

I have a List<T> defined below.
===========================================================
List<KeyValuePair<int, MyClass>> tciList = new List<KeyValuePair<int, MyClass>>();


I have the key<int> value for which I wish to return a match.
How do I return a KeyValuePair<> from the above list using Linq?


Thanks,
JohnB
Comment
Watch Question

Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi jxbma;

This is how it can be done.

Fernando
int myKeyVal = 3;

// Get the key value pair for a specific key
KeyValuePair<int, MyClass> kvp = (from k in tciList.AsEnumerable()
                                  where k.Key == myKeyVal
                                  select k).SingleOrDefault();

int key = kvp.Key;
MyClass value = kvp.Value;

Open in new window

jxbmaSoftware Consultant

Author

Commented:
Thanks so much, that is awesome.
An additional question.....

Currently, I'm searching for a matching value based on the key.
This is good.


Is there a way to extend this so that:

KeyValuePair<int, MyClass> kvp = (from k in tciList.AsEnumerable()
                                  where (k.Value as myClass).foo == myLookupValue
                                  select k).SingleOrDefault();

Basically, I want to only return rows where a field in MyClass matches some
sort of search criteria.  I could certainly iterate over the list, grab each instance
of myClass that matches my search criteria.


Thanks,
JohnB
jxbmaSoftware Consultant

Author

Commented:
Let's spin this around a bit. Let's say I want to be restrictive based
on a field based ony my value in the KeyValuePair. The examples listed
below are pseudocode & don't compile.
====================================================================


What would I do to select the number of instances that match my selection criteria?
I'm not sure that the v.Value as MyClass will necessarily compile.
---------------------------------------------------------------------------------------------------------
int count = (from v in this.ToList().AsEnumerable()
                     where (v.Value as MyClass).DiscountID == myDiscountID
                     select v).Count();


How would I extend/modify  this do to select select a list of entities KeyValuePairs<>
that match my criteria?
---------------------------------------------------------------------------------------------------------

Retired
Distinguished Expert 2017
Commented:
Hi jxbma;

You was not to far from it. See code snippet.

Fernando
int myDiscountID = 10;

int count = (from v in this.tciList.AsEnumerable()
             where (v.Value as MyClass).DiscountID == myDiscountID
             select v).Count();

Console.WriteLine("Count = " + count);

Open in new window

jxbmaSoftware Consultant

Author

Commented:
Thanks for the great help.
I'm just getting into the world of LINQ.

Check back I've opened another question on how to update values.

Thanks,
JohnB
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Not a problem, always glad to help.  ;=)