5

Starter code: https://dotnetfiddle.net/lJhMyo

    string[] names = { "Burke", "Laptop", "Computer", 
                       "Mobile", "Ahemed", "Sania", 
                       "Kungada", "David","United","Sinshia" };

     var empList =  new List<Employee> {
         new Employee {Name = "Burke", ID = "IHED123"},
         new Employee {Name = "David", ID = "QIUHD454"},
         new Employee {Name = "Batman", ID = "OIWQE565"},
     };

How do I construct a linq query (method syntax) that gets all Employee objects where employee name is in the "names" array?

If there is a string in "names" that is not in empList, throw exception.

EDIT: What if empList is large and I want case-insensitive match for Name?

0

3 Answers 3

5

You can use .Contains. ie:

var result = empList.Where(x => names.Contains(x.Name));

You can check if there is a missing name:

bool noneMissing = !names.Any(n => empList.Any(x => x.Name == n));
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks. I just edited the original post with this: What if empList is large and I want case-insensitive match for Name? What would be a more performant solution?
@JamesBurani, Linq converts Contains to an in query where there is a parameters count limit. If you are seeking for a performant search then it depends on your backend. For example for SQL Server, you would want to use a CLR UDF or something like Jeff Moden's 8K splitter function. Or alternatively, using SQL BulkCopy, you could send the names to a temporary table and do a server side join (Bulk copy is also available in some other backends). Linq is not really built for speed.
2

.Contains Tests if an array or list contains the item:

var Searched = emptList.Where(x => names.Contains(x.Name));

If Searched.Length == 0, so no Items

If you want performance use for loop (nothing is more performant) and for case insensitive use StringComparer.OrdinalIgnoreCase.

List<string> Searched = new List<string>;
for(int i = 0; i < names.Length; i++)
{
    if(emptList.contains(name[i], StringComparer.OrdinalIgnoreCase)
        Searched.Add(name[i]);
}

2 Comments

Hi, thanks. I just edited the original post with this: What if empList is large and I want case-insensitive match for Name? What would be a more performant solution?
If you want the more performant soltuion, forget Linq and use For Loop, its the most efficient. You could replace Array to Hashet to gain again, but be sure string is uniq
1

For large lists and case-insensitive comparison you can use HashSet with provided IEqualityComparer<string> :

var hashSet = new HashSet<string>(names, StringComparer.OrdinalIgnoreCase);
empList.Where(e => hashSet.Contains(e.Name));

And possibly moving from LINQ to for loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.