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

Wednesday, August 19, 2009

The simplest Month generator with c#, linq and anonymous types!

Just come up with a nice solution which i would like to share.

Lets say you want to generate a list of Month names in order to bind them to a DropDownList in an asp.net page.

The simplest way i came up with is the following:

DropDownListMonth.DataTextField = "Name";
DropDownListMonth.DataValueField = "Number";
DropDownListMonth.DataSource = Numbers(1,12).Select(Number=> new 
{
    Number,
    Name = (new DateTime(2000,Number,1)).ToString("MMMM")
});
DropDownListMonth.DataBind();


public static IEnumerable<int> Numbers(int from, int to)
{
    for (int i = from; i <= to; i++)
    {
        yield return i;
    }
}



So, Linq and anonymous types rocks!

Sunday, December 21, 2008

Sql Server Compact 3.5 Sp1 with Entity Framework

Writing for C# and .Net Framework 3.5, recently i needed a local database for a Desktop Application.

I tried to figure out which Database system is best for my case.
My needs was a small footprint database system with good interaction with Visual Studio or with good Tools which should have a .Net Sql provider and it would be Free or cost minimum.
There was a good article (i will update here when i find it again) referring
Sql Server Compact (CE),
Sql Server Express,
Vista DB,
Firebird,
SqlClient and more.

I ended to the 2 Microsoft's database systems, Sql Server Compact and Sql Express.
There is a good article (MS Word Document) from MSDN comparing these two database systems.

After reading that too i ended up to the Sql Server Compact as it runs in process with my desktop app, it uses a small memory footprint, it does not need an installation procedure (X-Copy) and it seems fast enough for my needs.
One think it doesn't have is stored procedures and views.
As i needed a factor of abstraction into my database system i searched for some DAL (OR/M) components to sit on top of my Database.
Currently there are these three choices:
NHibernate seems to be a good solution and compared to Entity Framework it is more mature, but for me being a Microsoft lover (Why use the Entity Framework?), i preferred to give a chance to Entity Framework with the add on of the Linq queries i like very much.
Linq to Sql seems more like a "Queries simplifier" to me than a real OR/M system.

So, i ended up using Sql Server Compact with Entity Framework and Linq queries.
Trying to harness these beasts the following links seemed most useful to me:

Regarding Sql Server Compact
Regarding Entity Framework: