score:2
Accepted answer
you need to make use of PredicateBuilder
, If i am getting correctly you are making single query and you need conditions in it
IQueryable<Product> SearchProducts (params string[] keywords)
{
var predicate = PredicateBuilder.False<Product>();
foreach (string keyword in keywords)
{
string temp = keyword;
predicate = predicate.Or (p => p.Description.Contains (temp));
}
return dataContext.Products.Where (predicate);
}
score:0
You could use the System.Linq.Dynamic package available on Nuget. You can also follow this guide on how to use the package.
In your case, I believe the query could be:
var query = IQueryable<DataType>();
foreach(string parameter in searchParameters)
{
query = query.Where("fieldName ==" + parameter );
}
// call the LINQ query at the end when all the conditions are built
var results = query.Select(q => q.field).ToList();
This may work for your case, otherwise you could use a predicate builder as per Pranay's suggestion.
Source: stackoverflow.com
Related Articles
- Build LINQ query in a loop
- Converting foreach loop to LINQ query breaks code
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Linq query built in foreach loop always takes parameter value from last iteration
- How to build a dynamic AND OR linq expression tree in a loop
- How to build a LINQ query from text at runtime?
- Dynamically build select list from linq to entities query
- How can I build Linq query with dynamic OR statements?
- The best way to build Dynamic LINQ query
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Linq query with multiple OrderBy statements added in a loop
- Convert async loop to LINQ query
- how to build case insensitive strong typed LINQ query in c#?
- LINQ Source Code Available
- Loop Through LINQ Query Columns (Not rows)
- Replace for-switch loop with a Linq query
- Dynamic Linq query - how do I build the select clause?
- linq - how do you do a query for items in one query source that are not in another one?
- How can I write the following code more elegantly using LINQ query syntax?
- How to build a nested query with the dynamic LINQ library
- How to dynamic add filters to a LINQ query against an Odata Source in C#
- Avoid extra loop and could not find implementation of query pattern for source type int Select not found
- C# Linq query help removing foreach loops creating cleaner code
- How to use expressions to build a LINQ query dynamically when using an interface to get the column name?
- Use a linq query as microsoft local report Data Source (WinForms)
- Determine the source DataContext for a Linq to Sql query
- LINQ query returns old results when source list is re-initialized
- How to get SQL query into LINQ form in C# code
- convert foreach loop to linq code
- Which way is better Linq Select Query or For Loop to search something in generic List?
- Search XML doc with LINQ
- LINQ Using inline subquery with min() in a left join where clause
- Linq group key is not unique
- Parameterise LINQ GroupBy
- how to use multiple order by in LINQ?
- return LINQ to list<entity>?
- asp.net core 2.1 ef core 2.1 date compare not working on server
- How to store a Linq query and use it later?
- Adding element to list with Lambda Expression
- Linq Grouping Using Two Constraints
- Linq expression for ILIst works in VB.Net but not C#?
- SQL Query to delete a comment and all its children in MySQL and Entity Framework
- How can I get only unique field values from a SharePoint CSOM query?
- Entity Framework method group Include does not work
- Returning recursive result
- Convert SQL query into Linq expression
- Filter generic list with generic dictionary
- MVC Razor Var data
- How to order a list of paragraphs
- Creating database using DataContext and System.Data.SQLite in C#