Tuesday, October 8, 2013

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 c0dem0nkey?

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 you IEnumerables to Lists so that you can manipulate your data more efficiently when needed.

Happy Coding >:]

-c0dem0nkey

Monday, September 16, 2013

Indexing SQL rows

Hi, this is another short blog post from before I hit the sack, so forgive it's bluntness.

Ever needed to return a set of data from SQL with index? One way of doing this is by the use of incremental counters, but one thing I've used was "Row_Number . Here's how to use it.

Select Row_Number () over (order by ID) AS Row,  Column1,  Column2,  from Employee
This query will give you an extra row with the name "Row" which will now serve as your index.

Hope this helps. Happy Coding!!

Monday, September 9, 2013

Removing List Items while using Foreach Loop

Ever used List.Remove while inside a For Each loop? If so, then you know that it will result to an error since the collection being iterated has changed.

foreach(var item in list)
{
 if(condition)
{
      list.remove(item)    //this will result in error 
}
}


Here's a tip for this case. Attach ".ToList()" .

foreach(var item in list.Tolist())

This way, the list will iterate in a list made by “ToList”. Now you can remove items from the original list without affecting your iteration.

Hope this helps. Happy Coding!

Sunday, September 8, 2013

CodeFirst Architecture: Sweet structure without the hassle

It's been a while since I last updated this blog. Last time, I mentioned to you some architecture that might be useful when building your software, but for this article, let me focus on CodeFirst. Now this architecture is based on Entity Framework which we will cover in another article.On the contrary, Entity Framework is Database-first design where you will be provided a .edmx file where your database tables and mapping will be maintained. It's a small price to pay compared to opening and closing your SQL connections via connection string. CodeFirst will not require you to maintain anything or any file for all you have to do is write Classes or Entities that you can manipulate. Data retrieval may be with the use of LINQ or SQL Query (at the very least amount)   I will not cover the steps entirely for it has already been covered by another article. I give credits where credit is due. We'll dig into more technical things on my next article. (Forgive me for this article was written while I'm travelling.)

Saturday, April 6, 2013

Spaghetti Codes

Spaghetti Codes

As developers, we've all encountered that chunk of code that seemingly came from the darkest pits of the underworld. They are repeatingly unstructured, complex and seemingly patched-up. In my experience, spaghetti codes spawn mainly as a result of mismanaged projects/coding tasks. Messed up timelines and cramming also contributes.

In our effort to somehow at least ease the pain of humanity by providing them with functional yet easy to understand code, we came across some technologies that will help in structuring your codes. Here's a list.

 Entity Framework.
Nhibernate with s#arp architecture
MVC or Model-View-Controller
MVP or Model-View-Presenter

These approaches has very common features with one another. They all use models to connect with SQL and Sharp Acrhitecture pretty much does a good job with layer segragation. LINQ is also a good way to reduce back-end scripting. . I'm pretty certain there are more but these are what I'm familiar with.

Hope this 15 mins blog of mine helps somehow.
-c0d3m0nkey

Wednesday, March 6, 2013

LINQ to SQL

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 (link here http://msdn.microsoft.com/en-us/library/vstudio/bb397926.aspx) as

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.

Hope this helps someone!
-c0dem0nkey

Monday, February 18, 2013

How to renumber primary key.


//Query below
 DBCC CHECKIDENT('', RESEED, 0)


The above line will reset your identity seed to 0, making the next insert's identity to 1. I'm pretty sure I have tried another way to do it but I cant recall it as of the moment. Hope this helps!

:) c0dem0nkey