C# 使用 Linq 搜索对象列表中是否存在值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1071032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:33:00  来源:igfitidea点击:

Searching if value exists in a list of objects using Linq

c#linq

提问by Tony_Henrich

Say I have a class Customerwhich has a property FirstName. Then I have a List<Customer>.

假设我有一个Customer具有属性的类FirstName。然后我有一个List<Customer>.

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

可以使用 LINQ 来查找列表中是否有一个客户Firstname = 'John'在单个语句中......如何?

采纳答案by zvolkov

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

LINQ 定义了一个非常适合解决这个确切问题的扩展方法:

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

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

确保您引用了 System.Core.dll,这就是 LINQ 所在的位置。

回答by Chris Brandsma

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

回答by M4N

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".

如果不存在名字为“John”的客户,则 john 将为 null。

回答by Jon Skeet

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

zvolkov 的回答是找出是否有这样一位客户的完美答案。如果您事后需要使用客户,您可以这样做:

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 firstsuch customer... to find all of them, just use a normal whereclause.)

我知道这不是你要问的,但我想我会先发制人的后续问题:)(当然,这只会找到第一个这样的客户......要找到所有这些客户,只需使用正常where条款。)

回答by Krassi

Another possibility

另一种可能

if (list.Count(customer => customer.Firstname == "John") > 0) {
 //bla
}

回答by jmservera

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

使用 Linq 你有很多可能性,这里有一个不使用 lambdas:

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

回答by Mike Sackton

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<string>{ "John", "Max", "Pete" };
bool has = customers.Any(cus => names.Contains(cus.FirstName));

or to retrieve the customer from csv of similar list

或从类似列表的 csv 中检索客户

string input = "John,Max,Pete";
List<string> names = input.Split(',').ToList();
customer = customers.FirstOrDefault(cus => names.Contains(cus.FirstName));

回答by Ian Boyd

The technique i used before discovering .Any():

我在发现之前使用的技术.Any()

var hasJohn = (from customer in list
      where customer.FirstName == "John"
      select customer).FirstOrDefault() != null;

回答by Fabio Stratotti

Try this, I hope it helps you.

试试这个,希望对你有帮助。

 if (lstCustumers.Any(cus => cus.Firstname == "John"))
 {
     //TODO CODE
 }