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.)