1

To simplify the problem, I suppose that I have a method with two boolean parameters getParamA and getParamB.

public JsonResult MyMethod(bool getParamA, bool getParamB)

Is There a way like a ternary operator or something to say if getParamA == true and getParamB == false for example, I create an anonymous object like this :

//this is an entityframework query 
var result = entityContext.MyTable.Select(r=> new 
{ 
      paramA = r.paramA // because getParamA = true
      // don't create paramB because getParamB is false  
});

I know it is easy to implement this using two parameters (using if else condition) but things are getting complicated if we have more than 5 paramters (because you need to do all the testing)...

Mehdi Souregi
  • 3,153
  • 5
  • 36
  • 53

1 Answers1

1

You can, but it isn't really efficient code. It makes your code a complete mess:

.Select( r => getParamA && getParamB
              ? (object)new { A = r.A, B = r.B }
              : (getParamA ? new { A = r.A }
                           : (getParamB ? new { B = r.B }
                                        : null
                             )
                )
       );

A better option might be the ExpandoObject, which uses a dictionary internally to store its properties and values.

dynamic eo = new ExpandoObject();

if (getParamA)
{
    eo.A = r.A;
}

if (getParamB)
{
    eo.B = r.B;
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Well, you are asking if it is possible, the answer is yes. Should you do it? No. – Patrick Hofman Jun 21 '17 at 12:46
  • @MehdiSouregi `ExpandoObject` then? – Patrick Hofman Jun 21 '17 at 12:48
  • Thanks, is it possible to implement it within the lambda expression above? – Mehdi Souregi Jun 21 '17 at 12:55
  • How would you create that expando object in expression (worse - this expression also represents a database query)? – Evk Jun 21 '17 at 12:57
  • @Evk What do you mean? Which query? – Patrick Hofman Jun 21 '17 at 12:58
  • `entityContext.MyTable.Select(r=> new ... )` < this one. While not specified explicitly - you can safely assume that is some entity framework or other ORM query, I think. – Evk Jun 21 '17 at 12:58
  • I think if I want to use the ExpandoObject i should get all the columns and after that I do the testing, I mean there is no way I can only get the parameters that I want. – Mehdi Souregi Jun 21 '17 at 12:59
  • @Evk Oh, yes. That is possible indeed. Then he has to materialize first, and then `Select`. – Patrick Hofman Jun 21 '17 at 13:01
  • @PatrickHofman, so there is no way I can get only the columns that I want ? – Mehdi Souregi Jun 21 '17 at 13:04
  • With EF? Then you have to build the expression yourself depending on your parameters. – Patrick Hofman Jun 21 '17 at 13:05
  • Something like what is demonstrated [here](https://stackoverflow.com/q/35346630/993547). – Patrick Hofman Jun 21 '17 at 13:05
  • 1
    Wow, as the answerer said in your link, the implmentation is too overcomplicated :p so I am going to get all the columns and for my case it happens to be that all the parameters on the entry are the exact same parameters on the table. I can not use ExpandoObject for my case because it is not serializable, so maybe i am going to use an IDictionnary – Mehdi Souregi Jun 21 '17 at 13:14
  • 1
    @MehdiSouregi ExpandoObject is serializable to json (for example by JSON.NET). Besides - it already implements `IDictionary`. – Evk Jun 21 '17 at 13:32