c# - 使用lambda C#从列表中的列表返回一个整数

标签 c# linq

我有以下类(class):

public class Customer
{
    public int location { get; set; }
    public List<int> slots { get; set; }    
}

然后我有一个客户列表:

List<Customer> lstCustomer = new List<Customer>();

然后我有一个插槽号:

int slot = 4;

我想返回插槽所属的特定位置的整数。 (见上面的客户类别)

这是我目前所拥有的:

int? location = lstCustomer
  .Where(l => l.slots.Any(x => slot))
  .FirstOrDefault();

但这不起作用(错误:无法将 int 转换为 bool)。任何帮助,将不胜感激。谢谢。

最佳答案

int? location = lstCustomer.FirstOrDefault(x => x.slots.Contains(slot))?.location;

关于c# - 使用lambda C#从列表中的列表返回一个整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50511262/

相关文章:

c# - 尝试使用JSON.NET和DataContractJsonSerializer反序列化JSON失败

c# - 使用 XElement 作为模板将 DataTable 转换为 XML

c# - 在 LINQ 中查找 2 列的乘积之和

c# - 林克/C# : How to split List into variable length chunks based on list item information?

c# - 我怎样才能确定有多少字符串适合一定的宽度?

c# - 当前上下文中不存在名称 'ConfigureAuth'

c# - 在字符串列表中选择一个字符串 linq

xml - XElement.Descendants () 使其不区分大小写

c# - EF Core 在映射到 Select 中的对象时查询 SQL 中的所有列

c# - 在 mvc 3.0 及更高版本中实现验证码