I need to filter my list based on multiple columns. here is my class that I need to search
public class psdata
{
public int ID { get; set; }
public string Customer { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public decimal? Identification { get; set; }
public decimal? Assesment { get; set; }
public decimal? PayoffLedger { get; set; }
public decimal? RentRestrictions { get; set; }
public decimal? CCR { get; set; }
public decimal SubTotal { get; set; }
}
And here is my linq query that looks at my list and then will find the search term if it is contained in the column.
var sl = list.Where<psdata>( c => c.Address.ToLower().Contains(text));
This works great for the first column. However if I want to add another column as so.
var sl = list.Where<psdata>( c => c.Address.ToLower().Contains(text) || c => c.City.ToLower().Contains(text));
I get an error
Operator || cannot be applied to operands of the type bool and psdata.
What am I doing wrong here?
Your only issue is the second c =>. You're still only passing a single Lambda expression, it just needs to include the || operator:
var sl = list.Where(c => c.Address.ToLower().Contains(text) ||
c.City.ToLower().Contains(text));
var sl = list.Where<psdata>( c => c.Address.ToLower().Contains(text) || c => c.City.ToLower().Contains(text));
should be
var sl = list.Where<psdata>( c => c.Address.ToLower().Contains(text) || c.City.ToLower().Contains(text));
You don't need the second 'goes to' => operator in your expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With