What is difference between IEnumerable and IQueryable?

There are following differences between ADO.NET and Entity Framework: IEnumerable IQueryable Exists in System.Collections Namespace Exists in System.Linq Namespace Best to query data from in-memory collections like List, Array etc. Best to query data from out-memory (like remote database, service) collections. While querying data from database, IEnumerable execute select query… Continue reading

How LINQ query is compiled to Expression Trees?

In LINQ, a query expression is compiled to expression trees or to delegates, depending on the type that is applied to query result. The IEnumerable<T> type LINQ queries are compiled to delegates and IQueryable or IQueryable<T> type LINQ queries are compiled to expression trees. In short, LINQ queries that execute… Continue reading

How Expressions Trees are different from Lambda Expression?

A common misunderstanding is that expression trees are identical to lambda expressions. But this is not true since you can also create and modify expression trees by using API methods i.e. without using lambda expression syntax at all. Also, every lambda expression cannot be implicitly converted into an expression tree…. Continue reading

How can you create Expression Trees?

You can create expression trees by using following two ways: Using Expression Lambda – The easiest way to generate an expression tree is to create an instance of the Expression<T> type, where T is a delegate type, and assign a lambda expression to this instance. // Create an expression using… Continue reading

What is Expression Trees?

Expression Trees was first introduced in C# 3.0 (Visual Studio 2008), where they were mainly used by LINQ providers. Expression trees represent code in a tree-like format, where each node is an expression (for example, a method call or a binary operation such as x < y). You can also… Continue reading

What is Expression?

In .NET, Expression is an abstract class which contains static methods and inherited by various types (like ParameterExpression, MethodCallExpression, BinaryExpression etc.) to create expression tree nodes of specific types. Also all these expression-specific expression tree types are defined in System.Linq.Expressions namespace.

How to use Having in LINQ?

There is no having operator in LINQ, but you can get the same result by using a Where clause after a Group By clause. DataContext context = new DataContext(); var q = from ord in context.tblOrder group ord by ord.CustomerID into grouping where grouping.Count() >= 2 select new { grouping.Key,… Continue reading

Write LINQ query to find nth highest salary?

You can find nth highest salary by writing the following query. DataContext context = new DataContext(); var q = context.tblEmployee.GroupBy(ord = > ord.Salary).OrderByDescending(f = > f.Key).Skip(1) //second, third ..nth highest salary where n=1,2…n-1 .First().Select(ord = > ord.Salary).Distinct();  

How to write LINQ query for Inner Join with OR condition?

Sometimes, you need to apply inner join with or condition. To write query for inner join with or condition you need to use || operator in where condition as shown below: DataContext context = new DataContext(); var q = from cust in context.tblCustomer from ord in context.tblOrder where(cust.CustID == ord.CustomerID… Continue reading