My Distinct query is working fine, but I need to put a WHERE clause in another method.
This is my working query for retrieving distinct managers
public static IEnumerable<DTO.Employee> GetDistinctMgrs()
{
IEnumerable<DTO.Employee> managers = EmpCache.EmployeeList.GroupBy(x => x.MgrID).Select(x => x.FirstOrDefault());
return managers;
}
Here is where I am trying to squeeze WHERE to get employee's manager from distinct list of managers. But with WHERE clause I get "Enumeration yielded no results"
public static IEnumerable<DTO.Employee> GetDefaultMgr(string EMPID)
{
IEnumerable<DTO.Employee> manager = EmpCache.EmployeeList
.Where(s => s.EmpID == EMPID)
.GroupBy(x => x.MgrID)
.Select(x => x.FirstOrDefault());
return manager; //<- "Enumeration yielded no results"
}
What you are doing is sending a reqest to the db and when the request is finished you do your groupby etc.
so when the db return nothing you get the exception
here is how i would do it
public static IEnumerable<DTO.Employee> GetDefaultMgr(string EMPID)
{
IEnumerable<DTO.Employee> manager = EmpCache.EmployeeList
.Where(s => s.EmpID == EMPID)
.GroupBy(x => x.MgrID);
if(manager?.Count()>0)
return manager.Select(x => x.First());
return null;
}
I would do it
public static IEnumerable<DTO.Employee> GetDefaultMgr(string EMPID)
{
IEnumerable<DTO.Employee> manager =
from mng in EmpCache.EmployeeList
where mng.EmpID.ToLower() == EMPID.ToLower()
orderby mng.MgrID
select mng;
return manager;
}
or
public static IEnumerable<DTO.Employee> GetDefaultMgr(string EMPID)
{
IEnumerable<DTO.Employee> manager = EmpCache.EmployeeList
.Where(s => s.EmpID.ToLower() == EMPID.ToLower())
.GroupBy(x => x.MgrID)
.Select(x => x.FirstOrDefault()).ToList();
return manager; //<- "Enumeration yielded no results"
}
Based in your new question
public static string GetDefaultMgr(string EMPID)
{
string managerID = EmpCache.EmployeeList
.Where(s => s.EmpID.ToLower() == EMPID.ToLower())
.GroupBy(x => x.MgrID)
.Select(x => x.MgrID).FirstOrDefault().ToString();
return managerID; //<- "Enumeration yielded no results"
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.