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 Query
- How to create "inline if statement" with expressions in dynamic select for null checking
- How to create dynamic Linq Select Expression with anonymous objects
- Create dynamic LINQ expression for Select with FirstOrDefault inside
- How to create dynamic query with for loop?
- Linq projection how to check for null collection from select statement
- 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?
- C# Create object with dynamic properties : LINQ select List<object> values by property names array
- 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?
- How to read XML file to a Dictionary<String,List<String>> with empty strings for null elements
- how to deal with exception in LINQ Select statement
- 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 can I select using LINQ for an entry that contains a LIST with more than one row?
- How to make select statement Dynamic ? Linq
- Generating Dynamic Select Expression for entities with nested entity in it
- How to create regex for mathing linq statement NOT contains where clause?
- How do you create nested groups with linq via lambda/method expressions
- How do I overcome an "Unable to create a constant value of type ..." exception for a simple WHERE statement
- How do I create a custom Select lambda expression at runtime to work with sub classes
- LINQ: How do I perform an inline select statement in a LINQ statement?
- How i can LINQ select where? with Filters if Null Values
- Dynamic LINQ expression for Select with child collection (Entity Framework)
- How do I create a variable Select Statement in LINQ
More Query from same tag
- EF and Linq with self referential table
- Entity Framework - Sorting Strings is causing "The wait operation timed out"
- NUnit: What is the most concise way to assert whether an IEnumerable contains an object of a certain type?
- Entity Framework Core Select Property with multiple conditions
- Convert LinqToXml expression to XPath
- Linq's Distinct() method isn't doing anything
- Populate a class with LINQ query
- LEFT JOIN in LINQ between dbContext and InMemory List
- How to specify to Regex (in a LINQ Query) to only compare the start of a string?
- Linq to XML compiled queries
- How to use EF Core extension methods asynchronously
- give type to anonymous type in linq query
- SQL to LINQ Conversion - Group by
- LINQ query works in LINQPad but in C#
- How can I do an EF Linq query, including a subset of related entities
- Compare two different types of list
- How to query in linq using orderbydescending?
- Issue related to Finding an Index of Item in List<string>
- difference of calling OrderBy() before Where() and vice versa
- .NET three columns
- EF Linq - Loading Relationship Values after Where Statement Part 2
- LINQ case sensitive
- Linq Group by and Order by
- Order by date in different directions
- Join and group 2 lists with common key
- Using XDocument to figure out if an element exists with a specific attribute?
- How to fix ViewState serialization error with my custom control?
- Add child to root XElement in XDocument
- Grouping Values With C# And LINQ
- Counting active listings by month and year