Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a dynamic where clause for dynamic keywords or using IQueryable C# Linq

I am trying to write a dynamic where clause using LINQ for returning all rows that contain any keyword supplied in an string array. The results do not come back as expected with what I have so far and looking at the SQL I can see the problem.

IQueryable<comments> query = _db.comments;

if (score != null)
    query = query.Where(x => x.score == score);
if (dateFrom != null)3
    query = query.Where(x => x.date_created >= dateFrom);
if (dateTo != null)
    query = query.Where(x => x.date_created <= dateTo);
if (keywords != null)
{
    //how to use OR for each in array?
    foreach (var keyword in keywords)
    {
        var keywordCondition = keyword;
        query = query.Where(x => x.text.Contains(keywordCondition));  
    }

}

WHERE ([Extent1].[score] = @p__linq__0) 
AND ([Extent1].[date_created] >= @p__linq__1) 
AND ([Extent1].[date_created] <= @p__linq__2) 
AND ([Extent1].[text] LIKE @p__linq__3 ESCAPE '~') 
AND ([Extent1].[text] LIKE @p__linq__4 ESCAPE '~')

--should be

WHERE ([Extent1].[score] = @p__linq__0) 
AND ([Extent1].[date_created] >= @p__linq__1) 
AND ([Extent1].[date_created] <= @p__linq__2) 
AND (([Extent1].[text] LIKE @p__linq__3 ESCAPE '~') 
    OR ([Extent1].[text] LIKE @p__linq__4 ESCAPE '~'))

I hope someone can help me as I've spent a couple of hours now searching for a solution.

Thanks in advance

like image 420
Rob C Avatar asked Apr 23 '15 13:04

Rob C


People also ask

What is IQueryable C?

IQueryable<T> is a C# interface that lets you query different data sources. The type T specifies the type of the data source that you're querying. Under the hood, IQueryable uses expression trees that translate LINQ queries into the query language for the data provided.

What is Dynamic LINQ?

The Dynamic LINQ library exposes a set of extension methods on IQueryable corresponding to the standard LINQ methods at Queryable, and which accept strings in a special syntax instead of expression trees.

When should I use IQueryable and IEnumerable using LINQ?

In LINQ to query data from database and collections, we use IEnumerable and IQueryable for data manipulation. IEnumerable is inherited by IQueryable, Hence IQueryable has all the features of IEnumerable and except this, it has its own features. Both have its own importance to query data and data manipulation.


2 Answers

As you have guessed, this is the part with the problem that you will need to change, because it will "AND" all of the clauses.

//how to use OR for each in array?
foreach (var keyword in keywords)
{
    var keywordCondition = keyword;
    query = query.Where(x => x.text.Contains(keywordCondition));  
}

I think you can do that by changing it to:

query = query.Where(x => keywords.Any(kw => x.text.Contains(kw));
like image 184
Alex Avatar answered Sep 29 '22 08:09

Alex


You should really look into Dynamic LINQ queries ( explained very well here - http://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library)

You can specify your query like below -

.Where("Column1 = value1 OR Column2 = value2");
like image 34
JunaidKirkire Avatar answered Sep 29 '22 08:09

JunaidKirkire