I am creating an object using Linq with the following code:
Model.Select(m => new { id = m.ProcessId, parent = m.ParentProcessId, text = m.Name });
However, I want to add in the following if
statement for the parent
property:
if (m.ParentProcessId == null)
parent = "#";
else
parent = m.ParentProcessId
Is there a way to do this inline with the Linq query? If not, how can this be done in a simple way?
CodePudding user response:
as in Documentation
The null-coalescing operator
??
returns the value of its left-hand operand if it isn'tnull
; otherwise, it evaluates the right-hand operand and returns its result. The??
operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null.
you can try the following
Model.Select(m => new {
id = m.ProcessId,
parent = m.ParentProcessId ?? "#",
text = m.Name });