score:2
The expression equivalent of the C# conditional ?:
operator is Expression.Condition
. While Expression.IfThenElse
you are using is the equivalent of C# if then else
block.
Both methods return ConditionalExpression
with Test
, IfTrue
and IfFalse
properties populated. The difference is that the result Type
of the Condition
is the type of the operands, while for IfThenElse
it is void
, hence cannot be used in query expression trees.
So the answer to your concrete question is:
var result = Expression.Condition(nullCheck, Expression.Constant(""), firstOrDefault);
P.S. As a side node, I'm getting several errors from your code snippet, so I had to rearrange it like this in order to get w/o error to the above line:
private static Expression GetLocalizedString(Expression stringExpression, SupportedCulture supportedCulture)
{
var expression = Expression.Parameter(typeof(APILocalizedString), nameof(APILocalizedString));
var prop = Expression.Property(expression, nameof(APILocalizedString.SupportedCulture));
var value = Expression.Constant(supportedCulture);
var condition = Expression.Equal(prop, value);
var where = Expression.Call(
typeof(Enumerable),
nameof(Enumerable.Where),
new Type[] { typeof(APILocalizedString) },
stringExpression,
Expression.Lambda<Func<APILocalizedString, bool>>(condition, expression));
var first = Expression.Call(
typeof(Enumerable),
nameof(Enumerable.First),
new Type[] { typeof(APILocalizedString) },
stringExpression);
var defaultIfEmpty = Expression.Call(
typeof(Enumerable),
nameof(Enumerable.DefaultIfEmpty),
new Type[] { typeof(APILocalizedString) },
where,
first);
var select = Expression.Call(
typeof(Enumerable),
nameof(Enumerable.Select),
new Type[] { typeof(APILocalizedString), typeof(string) },
defaultIfEmpty,
Expression.Lambda<Func<APILocalizedString, string>>(
Expression.Property(expression, nameof(APILocalizedString.Text)),
expression
));
var firstOrDefault =
Expression.Call(
typeof(Enumerable),
nameof(Enumerable.FirstOrDefault),
new Type[] { typeof(string) },
select);
var nullCheck = Expression.Equal(stringExpression, Expression.Constant(null, stringExpression.Type));
var result = Expression.Condition(nullCheck, Expression.Constant(""), firstOrDefault);
return result;
}
Source: stackoverflow.com
Related Articles
- How to create "inline if statement" with expressions in dynamic select for null checking
- C# Create object with dynamic properties : LINQ select List<object> values by property names array
- How to create dynamic Linq Select Expression with anonymous objects
- Create dynamic LINQ expression for Select with FirstOrDefault inside
- LINQ - Select Statement - The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type
- Dynamic linq order by on nested property with null properties
- Why C# LINQ expressions must end with Select or Group By Clause where as no such restriction in VB.Net
- How to create a dynamic LINQ select projection function from a string[] of names?
- How can I create a dynamic Select on an IEnumerable<T> at runtime?
- How can I create a dynamic multi-property Select on an IEnumerable<T> at runtime?
- How can I create a dynamic LINQ query in C# with possible multiple group by clauses?
- SELECT NEW with a potentially null field using LINQ and Entity Framework
- How do I define a SELECT TOP using LINQ with a dynamic query?
- how to create a pivot table with dynamic column using linq tree expression
- How to create a dynamic 'contains or LIKE' Expression to be used with Linq against OData service
- How do I do this SELECT statement that uses column renaming (or column alias) with LINQ?
- Creating a dynamic Linq select clause from Expressions
- Create a Dynamic Linq to EF Expression to Select IQueryable into new class and assign properties
- how to deal with exception in LINQ Select statement
- C# Dynamic LINQ: Select with Sum on dictionary indexer
- Query expressions over source type 'dynamic' or with a join sequence of type 'dynamic' are not allowed
- Select method missing from intellisense with Linq statement
- select from linq statement with await
- LINQ Join statement with possible NULL id?
- Select Distinct Row from Data Table with Dynamic Columns
- C# How to Create a Custom (dynamic) class with Dynamic Linq using XElement in runtime
- Stubbing Code for Test With Linq Expressions and Lambdas
- How can I create a dynamic select expression in LINQ?
- How to make select statement Dynamic ? Linq
- Generating Dynamic Select Expression for entities with nested entity in it
- Linq - Group by and count over multiple fields (output on single total)
- Linq How to combine List Items to other List items
- Transform LINQ IQueryable into a paged IQueryable using LINQ to NHibernate
- convert foreach and if to linq queries
- Linq query to return list within a list
- Doing a LINQ join, then group by, then a sum on two different columns
- Optimize filtering of dynamic list by looking at string properties
- c# linq add single quote before & after field values on datatable
- LINQ takes 40s to end query - Performance
- How to get the xml Element having optional value minOccurs=0
- How to get values after dictionary sorting by values with linq
- Where is IQueryable in the .NET Compact Framework?
- Returning the correct type in C# linq query over DataRows
- View result of LINQ query in watch/debugger
- How To Use Yield Inside A Linq Query
- Normalize or Denormalize: Store Contact Details (Phone Numbers) in separate Table? Search Performance?
- Is there a more concise way to express this foreach loop?
- Remove Duplicates From List<T> Based on Property Selected By Lambda
- How do I optimize checking for any added, deleted or updated items on a list with Linq to Objects?
- Modify asp.net grid via linq datasource