?: Operator in LINQ Query

后端 未结 5 2032
时光取名叫无心
时光取名叫无心 2021-02-20 15:20
  • How do I utilize a ?: operator in the SELECT clause of a LINQ query? If this can\'t be done, how can I emulate one? The goal is to get a CASE block in my select claus

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-20 16:12

    if you're checking just for null, you can also use ??

    string something = null;
    string somethingElse = something ?? "default value";
    

    As for the examples above, it is correct to do the ones that go...

    string something = (somethingElse == null ? "If it is true" : "if it is false");
    

    The parens aren't required, but they do aid in reading.

提交回复
热议问题