C sharp IEnuremable

I am trying to get a object out of IEnumerable collection which is in Dictionary.

I have a method which return IEnumerable of Employees. so In my code I am saying:

var emplist = employeelist.GetEmployeelist(ids);

Open in new window


I want a get a object depeding on the key i.e empid. So I am saying:

if(emplist.containKey(id)) { var emp = emplist.where(x => x.key == id);

Open in new window


so I am hoping that i would get the employee object in emp and can go though the emp object like emp.id, emp.name etc.

Could you please let me know how can i get the object from IEnumerable collection which is in Dictionary?
Ritesh JaiswalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Assuming the dictionary stores a value Employee (replace with the class name you use should it be different)

Employee emp;
if (emplist.TryGetValue(id, out emp))
{
    //Now you can use the returned employee
}
Ritesh JaiswalAuthor Commented:
Its complaining that Argument2: cannot convert from out Employee to out system.collections.Generic.IEnumerable<Employee>
Fernando SotoRetiredCommented:
Please post the actual code and how the dictionary is defined.
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Ritesh JaiswalAuthor Commented:
Ok so the method is defined like below which returns the Dictionary

Public static Dictionary<int, IEnumerable<Employee>> getEmployee(List<int> Ids){

}

and the code that consumes the return value is like below

var empList = getEmployee(ids)

After receiving the list I try to get the object from it like below

if(empList.containsKey(101)) {

Employee e;

if(empList.TryGetValue(101, out e))
{
 here i want to retrive value from e
e.name
e.salary
}
Fernando SotoRetiredCommented:
And when you do this
if(empList.TryGetValue(101, out e))
{
 here i want to retrive value from e
e.name
e.salary 
}

Open in new window

Does it return an exception? If so what is the exact message and inner exception if given.
Ritesh JaiswalAuthor Commented:
Compilation error ------  Argument2: cannot convert from out Employee to out system.collections.Generic.IEnumerable<Employee>
Fernando SotoRetiredCommented:
This is a single instance of employee, Employee e;, and can't hold a collection of Employee's.

Change Employee e; to this, IEnumerable<Employee> e;, and then you can enumerate through the collection.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
Why does getEmployee() return Dictionary<int, IEnumerable<Employee>>?  One would think that for any given ID, there would be only ONE Employee record?!

From our limited understanding it would seem to make more sense to return Dictionary<int, Employee>.  That way you pass in a List of IDS and get back a Dictionary that for any given ID returns just one Employee instance.
AndyAinscowFreelance programmer / ConsultantCommented:
A repeat of the first line of the original comment with extra highlighting:
Assuming the dictionary stores a value Employee (replace with the class name you use should it be different)

As you are storing an IEnumerable<Employee> then the out value expected is of type IEnumerable<Employee>.  (Note what your error message says).

However why are you storing IEnumerable<Employee> in the dictionary?  Should you just not store an 'Employee' ?
Fernando SotoRetiredCommented:
To your question, "Why does getEmployee() return Dictionary<int, IEnumerable<Employee>>? ", it does not. The dictionary is of type Dictionary<int, IEnumerable<Employee>> and the function getEmployee looks up the key in the dictionary which is an int and if found returns a collection IEnumerable<Employee>.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
I think we're talking about two different points in the code.  The getEmployee() function clearly returns a Dictionary.  My comment was more geared at why the value side of that returned dictionary isn't a single instance (instead of IEnumerable).  It's hard to guess how this is actually being used, but why would you have more than one instance of Employee per ID?  One way to fix it would be to change the declaration of "e", as you suggest, but it seems like we should be fixing it further back, changing the getEmployee() function to return what he's expecting in the first place.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.