0

What is the difference between the two pieces of code there?

And that is better than what is used?

DataClassesDataContext db = new DataClassesDataContext();
var QSelect1 = from _ADS in db.tblADs
                where _ADS.fldAdsId == Convert.ToInt32(e.CommandArgument)
                select _ADS;
var QSelect2 = db.tblADs.Where(x => x.fldAdsId == Convert.ToInt32(e.CommandArgument));
  • 3
    The first one will be converted to the second one when you compile your code. So the answer is they differ only in syntax and cost of compilation. – Hamid Pourjam Jan 28 '16 at 20:17
  • ... and for me, it's a preference to use the 2nd `method chain` syntax, rather than the former `query expression` -horses for courses – jim tollan Jan 28 '16 at 20:22

1 Answers1

2

The first one is query expression, and the second one is Lambda expression. They are equivalent. And they are the same in the end.

For more info see: Query Syntax and Method Syntax in LINQ (C#)

One thing to remember is that there are many operators that have no keyword in query syntax and you should use fluent syntax. This means any operator outside of the following:

Where, Select, SelectMany
OrderBy, ThenBy, OrderByDescending, ThenByDescending
GroupBy, Join, GroupJoin

You can also use a mix version of them:

int matches = (from n in names where n.Contains ("a") select n).Count();
Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110
  • 1
    almost - the 2nd one is `method chain` - which is more akin to lambda than the former, which looks like a close cousin of sql – jim tollan Jan 28 '16 at 20:26