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 Query
- 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?
More Query from same tag
- Multiple linq "from" and variables visibility
- Enumerating Descents using LINQ query
- mvc 5 asp.net selectlist with where condition
- DataTable to Dictionary<string, Dictionary<string,string>>
- Dynamically build up a linq query with hierarchy using string field names?
- Use dynamic linq or sql query in report generator
- Intersect is not providing any results using EqualityComparer
- Remove specific nodes under the XML root?
- Sort dictionary based on 2 factors
- LINQ : Take after GroupBy
- LINQ: Group by date including the year
- How does LambdaExpression resolve to Expression<Func<T, returnT>> at runtime via dynamic?
- Can't get result of desired query in linq
- Iterate over array with LINQ and use the value at each position
- include multiple objects inside an object [Entity framework]
- Entity Framework ignores Any clause
- How to get default path database location for a WPF application?
- Error in downloading a file in ASP.NET MVC
- When using LINQ shall we use 3 layers?
- increment each number in list by 1 using linq
- Access the contents of an IQueryable/System.Collections.Generic.List?
- Some suggestions on which .NET ORM to look at learning
- ASP.NET MVC Linq Join
- Is Linq included in .net 2010
- Linq - How to collect Anonymous Type as Result for a Function
- Linq query: does this array contain this string?
- How do I do this in LINQ
- Handling null values and missing columns in Linq-to-Xml
- Record just created in Model has null related table instead of empty which breaks my query?
- How to avoid memory overflow when querying large datasets with Entity Framework and LINQ