Usage of CONTAIN filter in LINQ queries

Standard

Contains scans a List. It searches for a specific element to see if that element occurs at least once in the collection. The C# List type has the useful Contains method that declaratively searches. It requires no explicit for-loop.

[HttpGet]
public IEnumerable<SelectListItem> GetBank(string countryName)
{
    List<SelectListItem> bankItems = new List<SelectListItem>();
    var lstLocations = _context.Locations.Where(x => x.Active_f == "A" && x.Country == countryName)
                                         .Select(x => x.Location_OID).ToList();
    if (lstLocations == null) return bankItems;
    bankItems.AddRange(_context.Banks.Where(e => e.Active_f && lstLocations.Contains(e.Location_OID))
                                     .Select(bank => new SelectListItem { Text = bank.BankName, Value = bank.Bank_OID.ToString() }));
    return bankItems;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s