Search code examples
f#query-expressions

Query Expressions to create SQL Case


Can you do SQL case with F# query expressions?

EntityFramework can do that in C# LINQ with

let x = condition1 ? "a" : condition2 ? "b" : "c"

Solution

  • F# query-expression is just an LINQ ExpressionTree (AST) so in deed it can be represented as let x = condition1 ? "a" : condition2 ? "b" : "c" in the expression tree but then getting the SQL-clause out is a separate task of how to translate that code to SQL. The let-binding in this example is actually the more complex one as LINQ starts to wrap these to kind of nested select clauses, and translating those expression trees to SQL is possible but not very easy.