If statement and assignments in lambda expressions

c# entity-framework lambda linq

Question

I have a lambda statement that has a mapping like this:

public enum Status
{
    Completed,
    InComplete,
    Ok
}

Query:

var courses = query.Select(c => new SomeModel
      {
          Status = c.someQuery() ? Status.Completed : Status.Ok
      });

So I want Status to have multiple if statements and not just a ternary operation. For eg.

var courses = query.Select(c => new SomeModel
      {
          Status = if(c.someQuery())
                   { 
                       return Status.Completed;
                   }
                   else if(c.someOtherQuery())
                   {
                       return Status.InComplete;
                   }
                   else if(c.someOtherQuery1())
                   {
                       return Status.Ok;
                   }
      });

So how do I accomplish something like this? I am using Entity framework ORM.

1
0
6/5/2012 1:05:33 AM

Popular Answer

You could nest your ternary operations:

Status = c.someQuery() ? Status.Completed : 
    c.someOtherQuery() ? Status.InComplete : Status.Ok 
5
6/5/2012 1:05:40 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow