Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter a list on multiple columns

Tags:

c#

asp.net

linq

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?

like image 590
Prescient Avatar asked Apr 14 '26 02:04

Prescient


2 Answers

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));
like image 114
Justin Niessner Avatar answered Apr 16 '26 01:04

Justin Niessner


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.

like image 24
jle Avatar answered Apr 15 '26 23:04

jle



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!