tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

Coalesce operator in Entity Framework

22 Feb 2009 1 mins .NET, Entity Framework, Firebird, LINQ

You may wonder how to use usefull coalesce operator in Entity Framework. If you look to some EF provider, i.e. for Firebird 😉 you will see, that there’s no coalesce related code. So you will end up, probably, with code like x => (x.BAR != null ? x.BAR : "N/A"). And that’s fun until you need longer chain of (not) null checks. But as Diego Vega pointed in some forum thread, you can use ?? operator. And the x => x.BAR ?? x.BAR ?? x.BAR ?? "N/A" looks, in my opinion, better.

But you have to be carefull, because in EF internally it’s handled same as first case, so using CASE. That means you may hit some query length limits of you database very easily. On the other hand, using it for normal stuff, you probably don’t need to care how it’s translated inside – important is, that the query is correct.

Profile Picture Jiří Činčura is .NET, C# and Firebird expert. He focuses on data and business layers, language constructs, parallelism, databases and performance. For almost two decades he contributes to open-source, i.e. FirebirdClient. He works as a senior software engineer for Microsoft. Frequent speaker and blogger at