-1
static private List<KeyValuePair<string, Employee>> employeesList = new List<KeyValuePair<string, Employee>>();

This is my generic KeyValuePair List that stores employee objects with employeeid as key. How to get value i.e Employee object with the help of the string key.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75

2 Answers2

0
using System.Linq;

Employee employee = employeesList.FirstOrDefault(x => x.Key == employeeId).Value;

You'd be better off using a Dictionary though. That way you could look up the Id directly rather than iterating the list.

Employee employee = employoeesDictionary[employeeId];
GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
0
var employee = employeesList.FirstOrDefault(x => x.Key == "").Value;

you can not use ? operator.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
Tolga Cakir
  • 725
  • 1
  • 7
  • 13