2

What is difference of

Linq: var data=from a in context.object select a;

EF: var data=context.object().Tolist();

Kinnal
  • 21
  • 5

4 Answers4

3

They are both LINQ.

The first is query (expression) syntax IEnumerable<int> numQuery1 = from num in numbers where num % 2 == 0 orderby num select num;

And the other is method syntax IEnumerable<int> numQuery2 = numbers .Where(num => num % 2 == 0) .OrderBy(n => n);

Source: MSDN: Query Syntax and Method Syntax in LINQ (C#)

Frank Fajardo
  • 7,034
  • 1
  • 29
  • 47
0

In your case the difference is that in first case data will be of lazy IEnumerable type and and will be executed on enumerating it.

Second example will enumerate colletion to a list and collection will be in memory after execution of ToList method.

But I assume you are interested in difference between LINQ operators like from, where, select and method Where, Select, etc. There is not difference as operators are compiled to methods.

Andrii Litvinov
  • 12,402
  • 3
  • 52
  • 59
  • Yes, Andrii, my confusion is we have two kind of syntax for EF one is linq and another is EF it self, so I got confusion that when to use which one? – Kinnal Apr 17 '17 at 05:40
  • @Kinnal, choose which ever you like best. They are identical at runtime. Personally, I prefer method call chain approach. – Andrii Litvinov Apr 17 '17 at 05:41
  • 1
    @Kinnal - it's not "ef it self" they are two different syntax's, that's it. Use which ever you prefer. LINQ or Lambda. – Phill Apr 17 '17 at 07:18
  • @Phill both are Linq queries, either Query or Fluent Syntax. In my view its confusing to call just one Linq. Lambda are integral part of the Linq Fluent syntax. – Mrinal Kamboj Apr 17 '17 at 07:49
  • @AndriiLitvinov what is `lazy IEnumerable`, I think you are pointing to deferred execution, which is also the case for IQueryable, in fact that's what both cases are, just in second call `ToList()` is called – Mrinal Kamboj Apr 17 '17 at 07:51
  • @MrinalKamboj, yes, you are right, I mean deferred execution. – Andrii Litvinov Apr 17 '17 at 07:57
0

Difference between both syntax is:

  • Your 1st query is LINQ to Sql and 2nd one is Entity SQL.

  • Entity SQL is processed by the Entity Framework Object Services directly

  • Entity SQL returns ObjectQuery instead of IQueryable

Dhananjay Singh
  • 161
  • 1
  • 12
0

You mean to know the kinds of difference in query syntax while Entity Framework.

Basics of LINQ & Lamda Expressions

LINQ

Linq is the Microsoft's first attempt to integrate queries into language. We know, it is really easy to find data from sql objects simply writing a query while its somewhat hectic when we want to do the same thing in a DataTable or Lists. Generally we will have to loop through every elements to find the exact match, if there is some aggregation we need to aggregate the values etc. Linq provides an easy way to write queries that can run with the in memory objects.

Lamda

A lambda expression is an anonymous function that you can use to create delegates or expression tree types. By using lambda expressions, you can write local functions that can be passed as arguments or returned as the value of function calls. Lambda expressions are particularly helpful for writing LINQ query expressions.

Hope it Helps.

Zaheer Ul Hassan
  • 771
  • 9
  • 24