Skip to main content

Posts

Showing posts with the label IEnumerable vs. IList

LINQ filtering Operators - where clause

The where clause is a filtering operator and used to filter a collections based on the some specific criteria and after that returns the values from collection based on your given criteria (returns those which are specified condition is true). It is accepts a predicate as a parameter. The where extension method has following two overloads, 1.       Func <TSource, bool> 2.       Func<TSource,  int, bool> A single LINQ query may contain multiple where clauses and a single clause may contain multiple predicate sub expressions. The multiple where extension methods are valid in a single LINQ query. For example, Multiple where clauses query, var query = base .Context.Carts.Where(x => x.EmailID == EmailID)         .Where(x => x.IsDeleted == false ) //APPLIED TO THE RESULT OF THE PREVIOUS.         .OrderByDescending...

Var vs. IEnumerable in LINQ

The “ var ” is a keyword that implicitly types a variable and it is strongly typed also. The “ var ” keyword derives type from the right hand side and its scope is in the method. The “ var ” is an implicitly typed local variable. We just let the compiler determine the type i.e.             var x = 1; //Implicitly typed.             int y = 1; //Explicitly typed. For example,      var customer = ent.Customers.Where(x => x.CustId>0).ToList();     IEnumerable < Customers > customer = ent.Customers..Where(x => x.CustId>0).ToList(); In the above example, the “ var ” keyword is only syntax for a programmer.  It doesn't change the semantics at all. If we declared as “ var ” the type of customer is still IEnumerable< Customers > and both the above query will generate the same output. Actually, ...

"How to Use" IEnumerable and IList [IEnumerable vs. IList]

When you should use IEnumerable and when IList? You should use IList when you need access by index to your collections and also when you need Add , delete , modify , ordering and / or positioning of your elements in the collections etc. You should use IEnumerable when you need to enumerate over your collection and represents a forward only cursor and also for querying data from in memory collections like Array , List , and collations etc. Stayed Informed - IEnumerable vs. IQueryable Actually, IList interface implements both the interfaces ICollection and IEnumerable and this interface allows us to add, remove, modify and ordering or positioning to items in the collections. The IList interface has more power than the preceding two interfaces ICollection and IEnumerable. The IList Interface contains as, 1.       Indexer 2.       Indexof Method 3.       Add Method 4...