0

I've seen examples where all of the following are referred to as LINQ.

var y = _context.Persons;
IQueryable<Person> x = _context.Persons;
var z = from tblPerson in _context.Persons
        select tblPerson;

Are they all LINQ, just different flavours of it or is only z LINQ? If so what are the other 2 called? If they're all LINQ how to I differentiate between the three when Googling for information about them? With y and x should I be using var or IQueryable in MVC CORE (and why) and should I be using either over z?

Glyn
  • 316
  • 2
  • 20
  • 1
    If you need `using System.Linq` to use it then it's LINQ. Otherwise it's not. – apokryfos Jul 28 '17 at 12:03
  • 1
    [This is LINQ](https://msdn.microsoft.com/library/bb308959). Going back to the basics (as in, the original concepts) may add some clarity. Note in particular the "LIN": Language INtegrated. A class with some neat features isn't language integrated on its own. Of course, all methods that are just there to help implement LINQ are also considered (part of) LINQ. – Jeroen Mostert Jul 28 '17 at 12:08
  • "Should I be using `var` or `IQueryable`" is a completely different question that has nothing to do with "what is LINQ". [See here](https://stackoverflow.com/questions/41479) for copious amounts of opinion on that subject. [Ditto for "should I be using x, y or z"](https://stackoverflow.com/questions/279701/). (However, `from a in q select a` specifically is *almost* entirely redundant and would be replaced with just `s` in most contexts, regardless of the provider involved.) – Jeroen Mostert Jul 28 '17 at 12:10

1 Answers1

1

Thanks to Jereon for the links. Wasn't sure if var or IQueryable was intrinsically linked to an implementation of LINQ or whether it was a more generic question. Generic it is.

From the links above this variety of LINQ

from tblPerson in _context.Persons
        select tblPerson;

is referred to as either

  • query syntax
  • query expression
  • query comprehension syntax

while this variety of LINQ

db.Person = _context.Persons
                    .Include(px => px.Gender)
                    .Include(px => px.Title)
                    .Include(px => px.AddressIDs)
                        .ThenInclude(px => px.AddressType)
.Where(px => px.Name.Contains("foo")
.OrderBy(px => px.Name);

is referred to as either

  • extension method
  • method syntax
  • fluent syntax

As for usage, the general opinion seemed to be in two camps

  • use one or the other throughout your project
  • mix-and-match use query syntax for the following

    • when using the let keyword
    • when you have multiple generators (from clauses)
    • when doing joins

and extension method for everything else.

Glyn
  • 316
  • 2
  • 20