Alex Rivera | Logout

How LINQ works internally?

Asked 2009-03-22T16:23:44.870
65

I love using LINQ in .NET, but I want to know how that works internally?

Edit
Report

1 Answer

3

In simple a form, the compiler takes your code-query and converts it into a bunch of generic classes and calls. Underneath, in case of Linq2Sql, a dynamic SQL query gets constructed and executed using DbCommand, DbDataReader etc.

Say you have:

var q = from x in dc.mytable select x;

it gets converted into following code:

IQueryable<tbl_dir_office> q = 
    dc.mytable.Select<tbl_dir_office, tbl_dir_office>(
        Expression.Lambda<Func<mytable, mytable>>(
            exp = Expression.Parameter(typeof(mytable), "x"), 
            new ParameterExpression[] { exp }
        )
    );

Lots of generics, huge overhead.

answered 2009-03-22T17:07:45.047

Your Answer