IEnumerable vs IList

In regards to our recent experience with the .ToList() function, it has come to our attention that we should have been using IEnumerable when gathering data from our Entities instead of returning a type of List in which we have to cast it using ToList() method. Why you ask? It is because using an object with type of IEnumerable gives us time to set aside the system’s data gathering until such that it is needed.

-What Are You Saying?

Think of this as something like Lazy Loading , where you put off data retrieval so that you can optimize the performance of your software where possible. Just remember to cast your IEnumerables to Lists so that you can manipulate your data more efficiently when needed.

Happy Coding >:]

IEnumerable vs IList

LINQ to SQL – .First() vs .FirstOrDefault()

I have an extra 30 mins for the day so I decided to make a quick blog about the first thing that I will remember from my early days of coding . For all you guys that is not that much familiar with LINQ or Language Integrated Query, let’s just say that it’s a feature introduced in Visual Studio 2008.

LINQ is defined by MSDN  here

Language-Integrated Query (LINQ) is a set of features introduced in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C# and Visual Basic. LINQ introduces standard, easily-learned patterns for querying and updating data, and the technology can be extended to support potentially any kind of data store.

In plain english, LINQ queries will let you query in Visual Studio without defining any stored procedure or string connections. An example is as follows

A basic SQL select like below

Select name from table1 where table1.id=1

will look like this in LINQ

var name=(from p in datacontext.table1
where p.id=1
select p.name).FirstOrDefault();

If we use .First() and the result list is returned empty, LINQ will throw an error. It assumes by saying .First() that it will return a result for it’s like saying SELECT TOP 1 in SQL.

LINQ to SQL – .First() vs .FirstOrDefault()