score:18
Accepted answer
You don't need the lambda expression in the "where" clause - the query expression translation does that for you. Just use:
var result = from rangeVariable in DataSource
where Foo.MethodReturnsBoolean(rangeVariable) == true
select rangeVariable;
I would personally then remove the "== true" redundancy (I know this was only sample code, but...):
var result = from rangeVariable in DataSource
where Foo.MethodReturnsBoolean(rangeVariable)
select rangeVariable;
I'd then consider what using a query expression is actually buying you. If you're just doing a "where" (or just doing a "select") you may find dot notation simpler:
var result = DataSource.Where(x => Foo.MethodReturnsBoolean(x));
It gets even better though: the compiler doesn't need to infer a return value from the lambda expression (because it will always be bool
) so you can just use a method group conversion:
var result = DataSource.Where(Foo.MethodReturnsBoolean);
How much cleaner is that? :)
Source: stackoverflow.com
Related Articles
- Linq Evaluating a method as a lambda expression
- Anonymous methods vs Extension method vs Lambda expression vs Linq
- Build LINQ Lambda Expression with contains method for enum property with list of int
- How to define in the lambda expression as another function in LINQ Select Method
- What is the best way to find length of split characters from the given string by using String.Split() Method or Linq Lambda Expression in C#
- C# Pass lambda expression field into method and use field in linq query
- Change lambda expression to a method - LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- C# Pass Lambda Expression as Method Parameter
- "Or" equivalent in Linq Where() lambda expression
- LINQ - Query syntax vs method chains & lambda
- LINQ: Passing lambda expression as parameter to be executed and returned by method
- Like in Lambda Expression and LINQ
- What is the Efficiency and Performance of LINQ and Lambda Expression in .Net?
- How to retrieve last 5 records using LINQ method or query expression in C#
- Selecting multiple columns with linq query and lambda expression
- How to Convert LINQ Comprehension Query Syntax to Method Syntax using Lambda
- LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method, and this method cannot be translated into a store expression
- convert this LINQ expression into Lambda
- Can you reverse order a string in one line with LINQ or a LAMBDA expression
- What is equivalent to clause between, for comparasion strings in LINQ or lambda expression of?
- LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression
- C# linq expression in lambda with contains
- Comparison : LINQ vs LAMBDA Expression
- Concatenate two column values in LinQ Lambda Expression
- Using a LINQ ExpressionVisitor to replace primitive parameters with property references in a lambda expression
- Equivalent of SQL Between Statement Using Linq or a Lambda expression
- LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression
- LINQ naming Standard - Lambda Expression
- Convert SQL query into Linq expression
- How to query an array of integers in an integer collection in ASP.NET Core 3.1?
- Most efficient way to get MAX value of joined table in LINQ query using C#
- Check if XML element exist using LINQ
- query nested objects with LINQ
- Create select list in MVC application using peta poco ORM
- IEnumerable<T> to Dictionary<string, IEnumerable<T>> using LINQ
- Datatable null to empty string
- How to join two tables with null values in referenced fields
- how to use like operator in lambda expression
- stop sql insert in a for or while loop
- How to remove list of kvp from a list of kvp using LINQ?
- Best practice to apply location filters
- Solution to rank positions in my LIST?
- Linq subquery same table using lambda
- Edit XML values in C#
- "An expression tree may not contain an assignment operator" using Aggregate in a Select clause
- Linq optimisation within a foreach
- Findcontrol in listview itemtemplate
- C# Store A List As a Comma Separated String In String Type Variable With Number Of Occurrence Of Items In The List