5 Replies - 1804 Views - Last Post: 28 December 2011 - 01:35 PM Rate Topic: -----

#1 insanepenguin  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 238
  • Joined: 08-July 10

Is it better to use LINQ extension methods or query operators?

Posted 28 December 2011 - 08:49 AM

Hi everyone,

I have recently discovered LINQ and it's great! I have been shown examples using LINQ extensions and query operators
i.e

 Console.WriteLine("List of Departments");
           // var depts = (empTree.Select(d => d.Department).Distinct());
            var depts = (from d in empTree
                         select d.Department).Distinct();
            foreach( var d in depts)
            {
                Console.WriteLine("Department {0}", d);
            }



I know SQL so query operators are nice but as I'm new to LINQ I don't mind learning one over the other if it is considered better practice.

Also,

Can you use LINQ instead of creating SQLAdapters, SQLCommands, DataSets etc? My project is going use databases a lot and I've heard LINQ can help with development speed

Many thanks and Happy New Year!

Mike

Is This A Good Question/Topic? 0
  • +

Replies To: Is it better to use LINQ extension methods or query operators?

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,457
  • Joined: 08-June 10

Re: Is it better to use LINQ extension methods or query operators?

Posted 28 December 2011 - 09:11 AM

Well, the query comprehension syntax covers a subset of the extension methods, so it's fair to say that you can do more with the extension methods.

On the other hand, sometimes it's easier to express things with the query comprehension syntax, for instance, defining range variables (using the let keyword), so it's also fair to say that you can express certain queries easier with the comprehension syntax.

So, there's no better way, just whichever way expresses things the way you want them to be expressed. Frequently, for simple things, the extension methods are shorter, since they don't always require a select statement. In your example it still would, but in many others it wouldn't. For example:

var q = (from x in collection
         where x > 10
         select x).Distinct();


Could be rephrased as:

var q = collection.Where(x => x > 10).Distinct();


As to your second question, yes, you can use LINQ for querying databases. There's lots of stuff about it on the MSDN, but a lot of it's outdated or needlessly complicated. I wrote a simple primer to get people started with LINQ to SQL:

http://www.dreaminco...-to-sql-primer/

There's also Entity Framework, which is probably where you should focus. In many ways it's similar to Linq to SQL, but it's more supported, and supports more databases. It's what MS is going to support in the future. It's also heavier and more complicated than Linq to SQL. I still use L2S for simple stuff.
Was This Post Helpful? 0
  • +
  • -

#3 insanepenguin  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 238
  • Joined: 08-July 10

Re: Is it better to use LINQ extension methods or query operators?

Posted 28 December 2011 - 11:57 AM

Thanks Curtis,

Will LINQ still be supported in the future for querying collections rather than databases?

I have this chapter coming up Displaying and Editing Data by Using the Entity Framework and Data Binding
Hopefully this will teach me enough about Entity Framework :)
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,457
  • Joined: 08-June 10

Re: Is it better to use LINQ extension methods or query operators?

Posted 28 December 2011 - 12:14 PM

Understand there's a difference between "LINQ" and "LINQ to SQL". L2S is a provider for LINQ that allows you to query databases. The extension methods provided in the LINQ library are here to stay.

By the way, EF also uses LINQ to perform it's queries.
Was This Post Helpful? 0
  • +
  • -

#5 insanepenguin  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 238
  • Joined: 08-July 10

Re: Is it better to use LINQ extension methods or query operators?

Posted 28 December 2011 - 12:17 PM

View PostCurtis Rutland, on 28 December 2011 - 12:14 PM, said:

Understand there's a difference between "LINQ" and "LINQ to SQL". L2S is a provider for LINQ that allows you to query databases. The extension methods provided in the LINQ library are here to stay.

By the way, EF also uses LINQ to perform it's queries.


Cool, so it's something worth learning :D
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,351
  • Joined: 29-May 08

Re: Is it better to use LINQ extension methods or query operators?

Posted 28 December 2011 - 01:35 PM

The query operators are mapped down to the LINQ extensions (in case of LINQ to Objects) or passed on to LINQ Provider.

In c# you do need the select statement in the query operators, vb.net doesm't

Dim q = From x In collection
        Where x > 10
        Distinct


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1