Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join columns in LINQ and run contains operator

I'm trying to create a search method where I'm wanting to check whether the keywords is contained within a any of number of different columns for a given record.

My Linq statement is as follows:

string[] searchFilter = {"john", "iceberg"};


var q = from qua in qual
                    join del in deliverables on qua.ID equals del.Q_ID
                    where searchFilter.All(s => (qua.Name + " " + qua.Project + " " + qua.Summary + " " + del.Name + " " + del.Summary).ToLower().Contains(s))
                    select qua;

I'm however getting an error message which states: "Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator."

like image 751
stats101 Avatar asked Dec 29 '25 03:12

stats101


2 Answers

Indeed you can't use the All method but just the Contains method:

var q = from qua in qual
        join del in deliverables on qua.ID equals del.Q_ID
        where searchFilter.Contains(qua.Name) ||  searchFilter.Contains(qua.Project)...
        select qua;

Of course you can combine your linq query dynamically with expressions (but it could be much more work), or use dynamic linq.

like image 89
Amiram Korach Avatar answered Dec 30 '25 17:12

Amiram Korach


Don't think you have choice :

But as you stay in a "Queryable" world, this will generate only one sql request (ugly, but...)

var q = from qua in qual
        join del in deliverables on qua.ID equals del.Q_ID
        select new{ qua, del};

foreach (var filter in searchFilter)
     q = q.Where(v => (v.qua.Name + " " + v.qua.Project + " " + v.qua.Summary + " " + v.del.Name + " " + v.del.Summary).ToLower().Contains(filter));

var result = q.Select(p => p.qua);
like image 42
Raphaël Althaus Avatar answered Dec 30 '25 19:12

Raphaël Althaus



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!