Are there any sane analogues to LINQ (.NET) exists for Scala?
-
3Specific LINQ method matched to the scala equivalent : http://stackoverflow.com/questions/8104846/chart-of-ienumerable-linq-equivalents-in-scala – Noel Kennedy Jan 06 '12 at 13:13
-
LINQ for Scala (full API) & deferred execution support: https://github.com/nicholas22/propelS – Scooterville Jun 09 '12 at 21:24
-
2Also see: http://stackoverflow.com/a/8106548/192247. Might be useful. – missingfaktor Jun 26 '12 at 09:16
4 Answers
It depends on what exactly you mean by "LINQ". LINQ is many things.
The most obvious answer would be: just use the .NET port of Scala. It gives you full native access to everything in .NET, which obviously includes LINQ.
Unfortunately, the .NET port of Scala was dropped a couple of years ago. Fortunately, it was picked up again a couple of months ago, with official funding directly from Microsoft no less. You can expect a release sometime in the 2011/2012 timeframe.
Anyway, what is LINQ?
A couple of features where added to .NET and specifically C# and VB.NET for LINQ. They are not technically part of LINQ, but are necessary prerequisites: type inference, anonymous (structural) types, lambda expressions, function types (Func<T...>
and Action<T...>
) and expression trees. All of these have been in Scala for a long time, most have been there forever.
Also not directly part of LINQ, but in C#, LINQ query expressions can be used to generate XML, to emulate VB.NET's XML literals. Scala has XML literals, like VB.NET.
More specifically, LINQ is
- a specification for a set of standard query operators
- a set of implementations for those operators (i.e.
IQueryable
, LINQ-to-XML, LINQ-to-SQL, LINQ-to-Objects) - a built-in embedded syntax for LINQ query comprehensions
- a monad
In Scala, like in pretty much any other functional language (and in fact also pretty much any other object-oriented language, too), the query operators are simply part of the standard collections API. In .NET, they have a little bit weird names, whereas in Scala, they have the same standard names they have in other languages: Select
is map
, Aggregate
is reduce
(or fold
), SelectMany
is flatMap
, Where
is filter
or withFilter
, orderBy
is sort
or sortBy
or sortWith
, and there are zip
, take
and takeWhile
and so on. So, that takes care of both the specification and the LINQ-to-Objects implementation. Scala's XML libraries also implement the collections APIs, which takes care of LINQ-to-XML.
SQL APIs are not built into Scala, but there are third-party APIs which implement the collection API.
Scala also has specialized syntax for those APIs, but unlike Haskell, which tries to make them look like imperative C blocks and C#, which tries to make them look like SQL queries, Scala tries to make them look like for
loops. They are called for
comprehensions and are the equivalent to C#'s query comprehensions and Haskell's monad comprehensions. (They also replace C#'s foreach
and generators (yield return
)).
But if you really want to know whether or not there are analogues for LINQ in Scala, you will first have to specificy what exactly you mean by "LINQ". (And of course, if you want to know whether they are "sane", you will have to define that, too.)

- 363,080
- 75
- 446
- 653
-
20The most useful part of the answer is "Select is map, Where is filter or withFilter, orderBy is sort or sortBy or sortWith,...". I am just missing Count(), First(), FirstOrDefault(). – Martin Konicek May 07 '11 at 09:40
-
12`size`, `head`, `headOption.getOrElse`.These are all in [scala.collection.Iterable](http://www.scala-lang.org/api/current/scala/collection/Iterable.html) – stevej May 08 '11 at 17:07
-
5One *huge* advantage LINQ has is that it supports deconstruction into Expression Trees which allows all the LINQ2SQL magic to "just work". Syntax aside, this level of expression tree integration is not currently available in Scala so all the "corresponding providers" must effectively also provide their own AST handling without this level of abstraction. – Jul 05 '12 at 20:51
-
@pst: yeah, you could currently probably do this with a VCR type `Dynamic` object that captures all method calls, but that's pretty brittle and a fair bit of complexity. Let's see what virtualized Scala and Macros bring to the table. – Jörg W Mittag Jul 06 '12 at 01:19
-
-
@chester89: Neither EPFL nor TypeSafe have anyone assigned to it, and the .NET backend will be removed from the Scala 2.11 distribution. – Jörg W Mittag Aug 21 '13 at 10:14
-
@JörgWMittag, that's sad. I'd be happy to help, but I guess my skills (especially in Scala) are not enough for a project of that magnitude :( – chester89 Aug 21 '13 at 14:34
-
What the OP meant by LINQ is clear and obvious. Would have been a great answer without being anal about the definition of LINQ. I mean really , who cares? – Apr 12 '16 at 13:08
All LINQ IEnumerable
extensions are available in Scala. For example:
Linq:
var total = orders
.Where(o => o.Customer == "myCustomer")
.SelectMany(o => o.OrderItems)
.Aggregate(0, (sum, current) => sum + current.Price * current.Count);
scala:
val total = orders
.filter(o => o.customer == "myCustomer")
.flatMap(o => o.orderItems)
.foldLeft(0)((s, c) => s + c.price * c.count)
-
@oleksii forAll and exists. http://www.scala-lang.org/docu/files/collections-api/collections.html – onof Mar 20 '15 at 13:07
Slick
is a modern database query and access library for Scala. (http://slick.typesafe.com/)
@table("COFFEES") case class Coffee(
@column("COF_NAME") name: String,
@column("SUP_ID") supID: Int,
@column("PRICE") price: Double
)
val coffees = Queryable[Coffee]
// for inserts use lifted embedding or SQL
val l = for {
c <- coffees if c.supID == 101
// ^ comparing Int to Int!
} yield (c.name, c.price)
backend.result( l, session )
.foreach { case (n, p) => println(n + ": " + p) }

- 473
- 1
- 5
- 13
There are many situations in Scala where you can use monadic constructs as a sort of query language.
For example, to query XML (in this case, extracting URLs from links in some XHTML):
def findURLs(xml: NodeSeq): Seq[URL] =
for {
a <- xml \\ "a"
href <- a attribute "href"
url <- href.text
} yield URL(url)
For an analogue of LINQ to SQL, the closest thing is probably ScalaQuery. To lift an example right out of the docs:
val q4c = for {
u <- Users
o <- Orders if o.userID is u.id
} yield u.first ~ o.orderID

- 121,135
- 26
- 193
- 155