score:13
This question is asked almost every day in some form.
The conditional operator type analysis proceeds from inside to outside, not outside to inside. The conditional operator does not know to what type its results are being assigned and then coerces the consequence and alternative to those types. It does the opposite; it works out the types of the consequence and alternative, takes the more general of those two types, and then verifies that the general type may be assigned.
The consequence and alternative contain no information about what the type of the lambda should be, and therefore the type of the conditional cannot be inferred. Therefore it cannot be verified that the assignment is correct.
It is edifying to consider why the language was designed that way. Suppose you have overloads:
void M(Func<string, int> f) {}
void M(Func<double, double> f) {}
and a call
M( b ? n=>n.Foo() : n => n.Bar() );
Describe how overload resolution determines which overload of M is chosen in a world where types are inferred from outside to inside.
Now consider this one:
M( b1 ? (b2 ? n=>n.Foo() : n => n.Bar() ) : (b3 ? n=>n.Blah() : n=>n.Abc()) );
Getting harder isn't it? Now imagine that Foo, Bar, Blah and Abc were themselves methods that took funcs, and also had arguments with conditional operators containing lambdas.
We do not wish to make the type inference process so complex without a corresponding huge benefit, and there is no such huge benefit for the conditional operator.
What you should do in your case is cast one or both of the consequence and alternative to the specific type.
score:1
fileTypeGroupID.HasValue ? (ContentItem n) => n.Document.MimeType.FileTypeGroupID == fileTypeGroupID.Value
: (ContentItem n) => true;
Note the conditional operator is the correct name for ? :
It's an example of a ternary operator (and is the only ternary operator in many programming languages).
score:2
This doesn't answer you question of why the compiler could not infer the type, but an easy work around would be to write your expression this way:
Expression<Func<ContentItem, bool>> expression =
n => !fileTypeGroupID.HasValue || n.Document.MimeType.FileTypeGroupID == fileTypeGroupID.Value;
Source: stackoverflow.com
Related Articles
- Can't use ternary operator to assign Linq expression
- Linq TypeScript Ternary Operator - is it possible to re-use the condition as the first expression without duplicating code?
- Using LINQ expression to assign to an object's property
- Ternary operator in LINQ where clause
- How to reuse a linq expression for 'Where' when using multiple source tables
- Conditional operator in Linq Expression causes NHibernate exception
- LINQ Source Code Available
- Ternary operator in Linq
- Create a Dynamic Linq to EF Expression to Select IQueryable into new class and assign properties
- Cannot use ternary operator in LINQ query
- Ternary Operator in LINQ Where clause for nullable bool column
- C# Dynamic Linq Ternary Operator
- creating Linq to sqlite dbml from DbLinq source code
- Linq to Sql: Optimizing lamba expression - clean code
- Ternary operator in LINQ query is not working as expected
- C# LINQ ternary operator as a switchcase inside a foreach
- linq lambda expression and '&' operator
- Add between clause in a LINQ expression with OR operator
- Is it the same to iterate over Linq expression result than to assign it first to a variable?
- Ternary Operator in LINQ Query
- How to assign multiple LINQ Include() statements to a variable for code re-use?
- How to use expression to assign property in LINQ Select new
- Ternary Operator Troubles with LINQ
- Unreachable expression code in LINQ query
- source code for LINQ 101 samples
- Linq Expression Refactor Duplicate Code
- Assignment to expression using ternary operator
- C# Linq filter data with ternary expression
- multiple groupby using operator in linq display differently from using expression
- optimise code into linq expression
- LINQ: How to convert a composite LINQ result to dictionary
- Where does the LINQ query syntax come from?
- LINQ Join needed?
- C# Linq Find duplicates with multiple group by
- How to regroup what's under a threshold with linq?
- Remove an object based on some condition
- Accessing Distinct values of List within List for ListView
- Translating SQL Query to Linq for use with Entity Framework
- How can I transform this object into a specific format nicely?
- how to get property nodes if the properties name is "frames"
- Does calling Select() or GroupBy() in Linq to entities trigger querying the database?
- Linq to sql and sql injection attacks
- methods that are returning different return types
- The best way to build Dynamic LINQ query
- LINQ with Group, Join and Where Easy in SQL not in LINQ?
- Linq multiple join if exits
- Function to linq conversion
- Multiple CheckBox Selection Result using Linq without hardcoding
- LINQ selection by type of an object
- Find row with max value for each key