Home > C#, Magiq > Magiq-to-sql: Creating annonymous types at runtime

Magiq-to-sql: Creating annonymous types at runtime

In a previous post about magiq, I faced a problem that would be solved generating a class at runtime. The idea was to create a linq query that creates a new instance of this class just to look the generated sql statement. Since this feature is fully supported by Dynamic Query (I mean, the generation of new classes in order to support the classic “select new { … }”), I looked the source code in order to see how it was implemented. Since I don’t want to recreate the wheel, I used the same code (I’ve done some refactoring due coding style and left the “Copyright (C) Microsoft Corporation. All rights reserved.” comment. I hope this is enough).
The great news about using this dynamic class is that I can retrieve all the values I need using one single select:
For the update

items.Where( condition ).Set( x => x.Count, x => x.Count + 2)
                        .Set( x => x.Name, "test" )
                        .Update();

and the insert

items.Where( condition ).Insert( x => new Item{ 
                                            Count = x.Count + 2 
                                            Name= "Test" 
                                           }).Into( items );

I have this matching select

items.Where( condition ).Select( x => new{ Count = x.Count + 2, Name = "Test");

This way I can handle everything with one single command generated by linq-to-sql :D. Indeed, I change the command text property of that command (and add some new parameters) so I can execute the update/insert without creating a new command.
Finally, the insert behaviour is pretty much the same as the update, so it was really easy to implement it once the update was done.
The first release of Magiq is coming 🙂

Advertisements
Categories: C#, Magiq Tags: , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: