TechHui

Hawaiʻi's Technology Community

Microsoft released VisualStudio 2008 last week. We now have a production version of C# 3.0 with LINQ. Using LINQ is much nicer than dealing with queries embedded in Strings. I really think Java is in dire need of something like LINQ. Thoughts?

Views: 762

Reply to This

Replies to This Discussion

I'd never heard of LINQ before, but it does look like something Java needs. Thanks for pointing it out. It reminds me of "list comprehensions" that I've seen in some examples of more functional languages. (I don't recall off-hand which language it was, though; Scala? JavaFx? JavaScript 1.7? Ruby?) But maybe the LINQ join goes beyond that?

Nice how they added it to their ORM framework too. I think IntelliJ has a plug-in that adds some language support for SQL strings, altho I'm not using SQL now and it sounds clunky so I haven't tried it. IntelliJ has some Hibernate support now too, but I haven't noticed any language help on the HQL strings.
> But maybe the LINQ join goes beyond that?

It does. It provides a generic way of performing queries over any datastructure including databases, in-memory collections, XML documents etc. I really like the way its integrated with the C# language.

> IntelliJ has some Hibernate support now too

I haven't looked at IDEA in a while. I'll have to give the latest release a spin.
Does LINQ provide static typing for those queries, e.g., based on database or XML schema? If not, then I wonder how much nicer it is than the stringy query DSLs like SQL or HQL.
Yes, LINQ produces strongly typed results when the type is knowable.
Example:

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
IEnumerable<int> lowNums = from n in numbers
    where n < 5 select n;

Using type inference I could also simply write this:

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums = from n in numbers
    where n < 5 select n;

Note this is not loose typing. Low nums is a typed enumeration in both cases.
That is cool, but I was wondering in particular whether and how LINQ knows the static types of the symbols in a database or XML query (such as SQL or XPath).
For databases it can simply use the column type. Lots of classes in .NET do this including the strongly typed DataTable.

LINQ to XML is not strongly typed, but there is a LINQ to XSD in beta which allows you to query XML with strong typing.
Thanks for that great explanation, Dave! It really helped me understand LINQ to XSD. I found XmlBeans and used it at a startup 4 years ago. I liked it so much that I gave a presentation about it to the Honolulu Java User's Group. So, I can imagine how typing works in LINQ and how it helps development.

Those 5 supporting features added to .NET sound compelling, too. On the JVM, I wonder whether Java will keep up or some other language will overtake it. Are you moving to Ruby because of Rails? I'm trying to decide which JVM language to learn now.
Thank you for the great summary of LINQ to XSD.

> The other thing about LINQ that is interesting is that, in order to do LINQ,
> as set of underlying capabilities had to be added to the .NET languages.

Exactly. One of the reasons I would really like to see LINQ in Java is the need for these language features. Recently I have been working with Groovy, Ruby, Python and C#. No when I am coding in Java I really miss things like object initializers.
Groovy appears to have similar features. The GORM library (part of Grails, but can be used without Grails) is Groovy's/Grails' object relational mapping (ORM) implementation.
For a typesafe LINQ to Java approach consider Querydsl : http://source.mysema.com/display/querydsl/Querydsl

Querydsl supports JPA/Hibernate, JDO, SQL and Java Collections.
Hi Timo, Having taken a quick look at Querydsl, it looks like one of the better non-DSL programmatic query generators I've seen (i.e. system for building queries using Java method calls.) Nice work! Absent a LINQ-like integrated query DSL, this is probably the next best option.

Timo Westkämper said:
For a typesafe LINQ to Java approach consider Querydsl : http://source.mysema.com/display/querydsl/Querydsl

Querydsl supports JPA/Hibernate, JDO, SQL and Java Collections.
Hi Daniel.

Thanks very much! We take user feedback very seriously and try to make Querydsl as good as possible within the constraints of Java (6). Scala support might come in the future.

Try it out and see if it works for you ;)

Daniel Leuck said:
Hi Timo, Having taken a quick look at Querydsl, it looks like one of the better non-DSL programmatic query generators I've seen (i.e. system for building queries using Java method calls.) Nice work! Absent a LINQ-like integrated query DSL, this is probably the next best option.

Timo Westkämper said:
For a typesafe LINQ to Java approach consider Querydsl : http://source.mysema.com/display/querydsl/Querydsl

Querydsl supports JPA/Hibernate, JDO, SQL and Java Collections.

Reply to Discussion

RSS

Sponsors

web design, web development, localization

© 2024   Created by Daniel Leuck.   Powered by

Badges  |  Report an Issue  |  Terms of Service