Mehfuz's WebLog

Live crazy, think different!

Sponsors

News

Passionate about cutting edge technologies and facinated by the modern web and phone revolution.Currently working at Telerik Corporation, the leading .net component vendor.
Follow me


Articles


Projects

Introducing LINQ to M

Now , it happens that you go to McDonalds website and order an item for you. It could happen that price of the menu changed or new one comes to the menu. Now, generally it can happen that admin can go though wizard and loads of textboxes or dropdowns to make the changes or he/she can just write as if he/she is writing in some text editor and everything else is taken care of on behalf. Textual DSL is a powerful tool that might become the future of how we communicate to computer systems. Well, things are not that easy to convert human language to machine readable forms completely but its not bad to dream for the best :-).
 
In this post, i will show how you can change items using  M and then bind it to a datagrid using LINQ query. I will do it using sample data that shows price menu for Mc burgers.
 
Our first step is to define a constant that holds the M value format.
 
   1: const string MGraphCode = @"Burgers
   2: {   
   3:    { Code=""DCB001"",Name=""Double Cheese Burger"",Quantity=1,Price=5},
   4:    { Code=""Mc001"",Name=""Mc Chicken"",Quantity=1,Price=4}
   5: }"; 

 

Next, you need to load the grammar using the QueryContext :

   1: var mcContext = QueryContext.Instance.Load(MGraphCode);

And, finally you need to bind it to a datagrid:

   1: gridView1.DataSource = mcContext;  
   2: gridView1.DataBind();

Now, what will be case if you want to do some query before you bind it to the grid. In that case, you need to construct  a wrapper class that represents the entity and in our case, let’s call it McBurger which we will be passing as a generic type to the Load method.

   1: public class McBurger 
   2: {  
   3:   public string Code {get;set;}
   4:   public string Name { get; set; }   
   5:   public int Quantity { get; set; }  
   6:   public float Price { get;set;}
   7: }

Accordingly, you can use LINQ statement to query it like:

   1: var mcContext= QueryContext.Instance.Load<McBurger>(MGraphCode);   
   2:  
   3: var query = from mcBurger in mcContext
   4:             where mcBurger.Code == "Mc001"
   5:             select mcBurger;
   6:  
   7: // rest of the databinding code remains the same.
   8:  

So, we took the context and queried for specific menu and datagrid binding remains the same which brings an output like

image

Actually, Steve and I are pretty excited about this, the possibility of doing things with M is almost endless. Steve has made a more detail post on this and you can find it right at LINQ to M is available.  You can also download the library as well as the sample from Telerik labs and it points to here.

Finally, you can download the example that i have shown from here.  By the way, you will be needing Oslo May CTP to run this sample.

Hope that helps

Shout it
Posted: Jul 19 2009, 03:14 PM by mehfuzh | with 2 comment(s) |
Filed under: , ,

Comments

No Comments