LINQ analogues in Scala?

前端 未结 4 1120
盖世英雄少女心
盖世英雄少女心 2020-12-12 19:15

Are there any sane analogues to LINQ (.NET) exists for Scala?

4条回答
  •  甜味超标
    2020-12-12 19:38

    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
    

提交回复
热议问题