C sharp IEnuremable

Ritesh Jaiswal
Ritesh Jaiswal used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AndyAinscowFreelance programmer / Consultant

Commented:
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
}

Author

Commented:
Its complaining that Argument2: cannot convert from out Employee to out system.collections.Generic.IEnumerable<Employee>
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Please post the actual code and how the dictionary is defined.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Author

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 SotoRetired
Distinguished Expert 2017

Commented:
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.

Author

Commented:
Compilation error ------  Argument2: cannot convert from out Employee to out system.collections.Generic.IEnumerable<Employee>
Fernando SotoRetired
Distinguished Expert 2017

Commented:
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 Teacher
Top Expert 2009

Commented:
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 / Consultant

Commented:
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 SotoRetired
Distinguished Expert 2017

Commented:
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 Teacher
Top Expert 2009

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial