tags:

views:

1008

answers:

7

Say I have a class Customer which has a property FirstName. Then I have a List.

Can LINQ be used to find if the list has a customer with Firstname = 'John' in a single statement.. how?

+16  A: 

LINQ defines an extension method that is perfect for solving this exact problem:

using System.Linq;
...
    bool has = list.Any(cus => cus.FirstName == "John");

make sure you reference System.Core, that's where LINQ lives.

zvolkov
Any is good, I wonder how many developers use Count when they should use Any?
RichardOD
You can also do a case insensitive search:cus => cus.FirstName.Equals("John", StringComparison.CurrentCultureIgnoreCase)
jmservera
A: 

customerList.Any(x=>x.Firstname == "John")

Chris Brandsma
This does not answer the question "if" such an entry exists; it merely enumerates the values if they do exist. An extra step is needed to determine if this enumeration is nonempty.
Jason
Your linq is not correct, should be: from x in customerList ...
jmservera
Then change the Where to Any. Probably more philosophical for me. I rarely need to know if without caring which ones they are.@jmservera: you were right. I gave up LINQ a while back and now use Lambda exclusively.
Chris Brandsma
I don't mean to be pedantic when I say that using the lambda calls is still technically using LINQ. (In particular, you're using LINQ-to-Objects.) You're just using the method calls rather than language keywords.
Judah Himango
A: 
List<Customer> list = ...;
Customer john = list.SingleOrDefault(customer => customer.Firstname == "John");

john will be null if no customer exists with a first name of "John".

M4N
That will throw an exception if *more than one* customer is called John.
Jon Skeet
Thanks for the comment. I'll leave the answer as a partially correct example.
M4N
It's still valid in a scenario when you are sure there is 1 and you want an exception to be raised if more than one, so I think it is good that you didn't delete it.
RichardOD
+7  A: 

zvolkov's answer is the perfect one to find out if there is such a customer. If you need to use the customer afterwards, you can do:

Customer customer = list.FirstOrDefault(cus => cus.FirstName == "John");
if (customer != null)
{
    // Use customer
}

I know this isn't what you were asking, but I thought I'd pre-empt a follow-on question :) (Of course, this only finds the first such customer... to find all of them, just use a normal where clause.)

Jon Skeet
I'd point out that you might appreciate having done this later when it comes to debugging, if you find yourself suddenly curious which customer it was that fit the criteria.
mquander
A: 

Another possibility

if (list.Count(customer => customer.Firstname == "John") > 0) {
 //bla
}
s7orm
Its' preferable to use Any in this scenario.
RichardOD
Yeah, I didn't know, that Any() terminates if the element is found...
s7orm
A: 

Using Linq you have many possibilities, here one without using lambdas:

//assuming list is a List<Customer> or something queryable...
var hasJohn = (from customer in list
         where customer.FirstName == "John"
         select customer).Any();
jmservera
A: 

One option for the follow on question (how to find a customer who might have any number of first names):

List<string> names = new List { "John", "Max", "Pete" };
bool has = customers.Any(cus => names.Contains(cus.FirstName));
Mike Sackton