Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# query vs C# LINQ

Tags:

f#

This question is more relating to what seems to be verbosity and less clarity on the part of F# compared to C#. A relatively minor issue but one that is becoming quite annoying the more database queries I write.

In C# I can do the following with LINQ and Entity Framework:

 var q = (from row in db.LAYERS
          where row.LIST1.Field1 == PARAM
          select new MyClass( row.field1, row.field2, row.field3, row.field4)).ToList()

The point is here I am just building a new list of classes inside the query itself. Whereas in F# this does not seem to work and in order to get the same result I'm doing something like this:

    query { for row in db.LAYERS do 
        where (row.LIST1.Field1 = PARAM)
        select row.field1, row.field2, row.field3, row.field4 } 
        |> Seq.map (fun row -> new MyClass(row.field1, row.field2, row.field3, row.field4)

F# is requiring me to do a map on the query sequence to get the list I want.

like image 260
Richard Todd Avatar asked Apr 01 '13 14:04

Richard Todd


1 Answers

Although you didn't provide code that fails to compile, I'm guessing you just need to put your object creation expression in parentheses:

query { for row in db.LAYERS do 
        where (row.LIST1.Field1 = PARAM)
        select (new MyClass(row.field1, row.field2, row.field3, row.field4)) }
like image 112
MisterMetaphor Avatar answered Oct 03 '22 07:10

MisterMetaphor