score:0
Accepted answer
where !condition || predicate(b)
score:-1
void main()
{
var list=new int []{1,2,3,4};
var query=from l in listings.whereif(listhaselements(list),s=>list.contains(s.id))
select l;
query.dump();
}
public bool listhaselements<t>(ienumerable<t> it)
{
return it.any();
}
public static class ext {
public static iqueryable<tsource> whereif<tsource>(this iqueryable<tsource> source, bool condition,
expression<func<tsource, bool>> predicate)
{
return condition ? source.where(predicate) : source;
}
}
Source: stackoverflow.com
Related Query
- How can I use this extension method in a linq to entities query using query syntax?
- How can I use this Linq Query using it with a multiple parameter by Join tables
- How can I simplify Linq extension method invokation using reflection?
- How can i change this LINQ query to use my List<int> instead of the hard coded number?
- How to use Linq extension method with CodeFluent Entities template?
- Is there any way to encapsulate a simple LINQ query in an extension method that can be used with LINQ to Entities query?
- How can I use this T-SQL query in LINQ / Entity Framework?
- How can I use custom expressions in DevArt LINQ to Entities and also use query comprehension syntax?
- How can I reformat this to use the other kind of LINQ Query format?
- I have a LINQ statement that is partially query and partially method. How can I do this using one or the other?
- How to map multiple entities from LINQ query into a new entity using a common map method
- How can i fix this method using Linq so that the test (which should already work because the result is correct) works?
- Why does C# compiler create private DisplayClass when using LINQ method Any() and how can I avoid it?
- How to retrieve last 5 records using LINQ method or query expression in C#
- How to Convert LINQ Comprehension Query Syntax to Method Syntax using Lambda
- How to: Use async methods with LINQ custom extension method
- How can I call local method in Linq to Entities query?
- How can I convert this SQL Query into LINQ (OVER (PARTITION BY Date))
- How can I use LINQ to project this parent and children object model into a flat, single object?
- Linq to entities extension method inner query (EF6)
- LINQ: How to do JOIN using the linq extension method style on multiple fields?
- How can I query this hierarchical data using LINQ?
- How can I write the following code more elegantly using LINQ query syntax?
- Using an extension method on a base class in a LINQ query
- How to use expressions to build a LINQ query dynamically when using an interface to get the column name?
- How to convert this Linq query syntax to method syntax?
- Can you use LINQ extension method operators in an ASP.NET databinding expression?
- Use C# extension method as predicate in linq query
- How can I turn this 12-line method into a 1-line LINQ expression?
- Can I use the .Min() Linq extension method on a bool type?
More Query from same tag
- LINQ: Calling a Method from within linq?, my method returns an anonymous type
- EF Core LINQ GROUPBY Then Select to get more than one properties of the entity
- Where do I put the "orderby group.key" in this LINQ statement?
- LINQ - Where clause with Boolean method
- Linq modified get a error message New transaction is not allowed because there are other threads running in the session
- Recursive category and inner join using
- Returning 0 when count is applied in empty list using LINQ
- Return data from inner join without creating new object
- How to write a query in method syntax
- How to loop through the properties of anonymous type in a list
- How do I query list for any matches on item property?
- What's the best way to get a single random element from a List<>?
- LINQ - Previous Record
- Remove duplicates from a ListView column cast to List
- Can I optimize my LINQ query?
- C# Linq Intersect Hashset<T> in memory with IEnumerable<T> from filestream
- LINQ Select syntax returns all columns instead of selected ones
- Can you use Linq to find relationships between elements in a collection?
- Left join multiple IList<> using Linq
- Passing objects in the linq statement
- LINQ to Entities custom method
- Linq to XML query not picking up any values
- How to substitute Expression<Func<T,bool>> into where clause of linq to sql expression
- LINQ flavored IS IN Query
- Get LINQ query Result with column value based on DateTime column
- Difficulty with LINQ
- How do I query many-to-many relationship in NHibernate
- How to find Distinct in more than one column in LINQ
- How can I include a field from an included table in my LINQ expression?
- LINQ: Traverse up a hierarchy to retrieve parent hierarchy