Coalesce operator in Entity Framework
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.



There's 3 Comments So Far
March 1st, 2010 at 02:06
The link to Diego Vega’s site shows all of his posts. Do you remember the name of the article or when he published it?
March 1st, 2010 at 08:11
Diego mentioned it in forum, not on blog.
Who Linked To This Post?
Share your thoughts, leave a comment!