Question#1
The function is always failing to find any but the last name in the namesToSearch array.
Debug.Assert(CountEmployeesByName(new string[] { "Name1", "Name2", "NoName3" }) == 2);
It fails . it finds 0 why? Shouldnt it return 2? What do i make change in my code?
Well... you don't even need to create local
List<IEnumerable<employee>>
to be able to get count of employees which name is matched to the name of string in array. The body of function can be simplified this way:
return context.employees.Where(x=>namesToSearch.Any(y=>y==x)).Count();
I hope, that resolves your other issues.
[EDIT]
If you would like to provide custom comparison, you need to
implement the Equals method[
^].Then you'll be able to compare eployee's name no matter of upper/lower case.
At this moment, you can implement Equals method, using
String.Equals() method[
^].
For example:
return context.employee.Where(x=>namesToFind.Any(y=>String.Equals(y, x,StringComparison.OrdinalIgnoreCase))).Count();
Above code should return
3
.
[/EDIT]
[EDIT#2]
To return distinct values, use
Distinct() method[
^]:
return context.employee.Where(...).Distinct().Count();
[/EDIT#2]