Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning just one integer from a list within a list using lambda C#

Tags:

c#

linq

I have the following class:

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

Then I have a list of customers:

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

Then I have a slot number:

int slot = 4;

I would like to return an integer of a specific location that the slot belongs to. (See customer class above)

This is what I have so far:

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

But this does not work (Error: Cannot convert int to bool). Any help would be appreciated. Thank you.

like image 834
Robert Smith Avatar asked May 20 '26 06:05

Robert Smith


1 Answers

int? location = lstCustomer.FirstOrDefault(x => x.slots.Contains(slot))?.location;
like image 178
sander Avatar answered May 21 '26 18:05

sander



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!