How can I write the following lambda expression in one line?

captainsac

I want to fetch the records as follows

SearchResult.condition is null then fetch all the rows from Person

if SearchResult.condition is false then fetch the rows where PersonType column contains null value

if SearchResult.condition is true then fetch the rows where PersonType column contains non null value

 struct SearchResult
 {
     public string Name;
     public bool? condition; 
 }

 Expression<Func<Person, bool>> expression;
 if(condition==null)
 {
     expression= (a =>
        (SearchResult.Name==null || a.Name == SearchResult.Name)
     );
 } 

else if(condition.Value == true)
 {
    expression= (a =>
    (SearchResult.Name==null || a.Name == SearchResult.Name)
    && a.PersonType != null)
 } 
 else if(condition.Value == false)
 {
    expression= (a =>
    (SearchResult.Name==null || a.Name == SearchResult.Name)
    && a.PersonType == null)
 }

I want to write the expression in one expression instead of using if else conditions. Can u plz help me in it?

Eren Ersönmez

You could shorten as:

expression = a => 
    (SearchResult.Name == null || a.Name == SearchResult.Name) && 
    (SearchResult.condition == null || Search.condition == (a.PersonType != null));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I take a regular expression from the beginning of one line and copy it to the beginning of following lines?

How can I write an inner class as a lambda expression?

How can I write the length function in one line?

How can I write a regex to extract data preceding and following a specific expression in a text file?

How can I write regex for the following statements?

When I write to a file in C it writes in different lines. How can I write it in one line?

How can I write a lambda expression that reads two different columns in a CSV file?

How can I remove a variable of a lambda expression?

How can I simplify and Optimize this lambda expression?

How can I use lambda expression here?

How can I write an if block using lambda?

Can be convert following java code to Lambda expression?

I want to write the regex expression for the following text

How do I write a lambda expression that looks like a method?

How can I force a throw to be a statement and not an expression (in a lambda expression)?

How can I make a one line generator expression to generate these two different lists

How can I write the following octave code without a for loop?

How can I write the following code in a more efficient and pythonic way?

How can I write the following Javascript code snippet better?

how can i write this sql according to the following criteria

How can I write following code into a recursive method?

How can I properly write the following if statement with getLabel()

How can I write following vector to csv file in r

How can I write the following code in layered architecture and generic?

how can I write this assembly line differently?

How to write "AND NOT" in Linq Lambda expression?

How can i write for&if-else statement using only one line?

How can I write this in one line without duplicate dictionary name and key?

How can I write one line code instead of taking mean of each variable inside the group_by()?

TOP Ranking

HotTag

Archive