pages

Showing posts with label Linq. Show all posts
Showing posts with label Linq. Show all posts

Sunday, 23 April 2017

Iqueryable vs Ienumerable a comparison.

There is always a doubt in developers mind regarding usage of IQueryable and IEnumerable.

IQueryable extends IEnumerable. IQueryable should be used while working with remote databases. 

Well, here are some facts which can help resolving your doubt and can strengthen your decision-making as which one to use.






Some important points about Ienumberable<T>
  • It's read-only.
  • Iterates only in forward direction.
  • Doesn't supports adding or removing objects.
  • It's a base interface for any generic collection
When to use IEnumberable:
  • Working with read-only collection
  • Need to read objects in the forward direction.
  • Want to iterate collection using for-each.



Some important points about IQueryable<T>
  • It implements IEnumerable so the results can be iterated using for-each.
  • Best suited for querying against the remote data source.
When to use IQueryable
  • your data source is queryable.
  • Data needs to be applied 
  • The deferred way is needed to load the data.





IEnumerable
IQueryable
Namespace
System.Collections Namespace
System.Linq Namespace
Derives from
No base interface
Derives from IEnumerable
Supported
Supported
Not Supported
Supported
How does it work
While querying data from the database, IEnumerable executes a select query on the server side, load data in-memory on the client side and then filter data. Hence does more work and becomes slow.
While querying data from the database, IQueryable executes a select query on the server side with all filters. Hence does less work and becomes fast.
Suitable for
LINQ to Object and LINQ to XML queries
LINQ to SQL queries
Custom Query
Doesn’t support
Supports using CreateQuery and Execute methods
Extension method
parameter
Extension methods supported in IEnumerable takes functional objects.
Extension methods supported in IEnumerable takes expression objects, i.e., expression tree.
When to use
When querying data from in-memory collections like List, Array, etc.
When querying data from out-memory (like remote database, service) collections.
Best Uses
In-memory traversal
Paging
Table from code project

Friday, 6 December 2013

Distinct Keyword in LINQ

Few days back when I was trying to Filter my result set using Linq Distinct keyword I got into a situation. The keyword didn't worked for me, I was getting same result set with or without Linq.

I Goggled for the solution, But most of the solutions offered to user the Group By operator. which is as follows:

Take an entity that contains following fields:
id, Name.
public class MyDemo
{

public int id{get; set;};

public string Name {get; set;};

}
MyDemo ListData = new MyDemo
{
id=1, Name="Alpha";
id=2, Name="Beta";
id=1, Name="Alpha";
id=3, Name="Gamma";
}
MyDemo ListResult;


ListResult = (ListData.Select(t => t).GroupBy(x => x.Name).Select(y => y.First()).ToList()

Distinct Keyword is little tricky in Linq. At first it would not work same like Distinct in sql server Linq to Object behaves little weird because all are distinct object references so the other way round to this problem is use a GroupBy Operator.

We can get the solution to this problem by introducing an anonymous type to store the result and in that case we will get the desired result.

var result =  (from d in ListData select new { d.id, d.Name }).Distinct();