Sudheer Kumar

ASP.Net, C#, BizTalk, MSBuild, WPF, WCF, WF....
posts - 28, comments - 111, trackbacks - 16

My Links

News



Archives

Post Categories

Dynamic LINQ

There are ways LINQ can query completely dynamically from Database.

I had requirement to get a column value of a table in a generic and dynamic manner for a given key value.

Here I pass the table type (LINQ type) and key value to retrieve other column values of the table in a totally dynamic manner.

private string GetColumnValue(DataContext dc, Type tableType, Type keyColumnType, string keyColumnName, object objectValue, string getColumnName)

        {

            string getColumnValue = null;

 

            IQueryable queryableData = dc.GetTable(tableType).AsQueryable();

 

            //Create an expression that gets specific property

            // tbl.Where (item => item.PropertyName == propertyValue)

            ParameterExpression pe = Expression.Parameter(tableType, "item");

            Expression left = Expression.Property(pe, tableType.GetProperty(keyColumnName));

 

            //Convert the key value to the respective type

            Expression right = Expression.Constant(Convert.ChangeType(objectValue, keyColumnType), keyColumnType);

            Expression e1 = Expression.Equal(left, right);

 

            MethodCallExpression whereCallExpression = Expression.Call(

                typeof(Queryable),

                "Where",

                new Type[] { queryableData.ElementType },

                queryableData.Expression,

                Expression.Lambda(e1, new ParameterExpression[] { pe }));

 

            // Create an executable query from the expression tree.

            IQueryable results = queryableData.Provider.CreateQuery<MediaRequest>(whereCallExpression);

            //NOTE: Onlye IQueryable<T> has methods like FirstOrDefault<T>().

            //So do a for..each to get the object in IQueryable.

            foreach (object o in results)

            {

                //Use reflection to get the value property value

                try

                {

                    getColumnValue = o.GetType().GetProperty(getColumnName).GetValue(o, null).ToString();

                    break;

                }

                catch (Exception ex)

                {

                    //Log the exception

                }

            }

            return getColumnValue;

        }

  Example and Usage:

MyDataContext mediaDB = new MyDataContext("Server=Server;Database=DB;Trusted_Connection=true");

            string tableTypeString = "Data.Request, Data"; //This is the full name of the LINQ class that corresponds to a data table

            Type tblType = Type.GetType(tableTypeString);

            string fileName = GetColumnValue(mediaDB, tblType, typeof(int), "RequestID", 1), "FileName");

            string ipAddress = GetColumnValue(mediaDB, tblType, typeof(int), "RequestID", 2, "IPAddress");

           

 Hope this helps..!


Print | posted on Tuesday, November 25, 2008 6:35 AM | Filed Under [ .Net 3.0/3.5 ]

Feedback

Gravatar

# re: Dynamic LINQ

What is <MediaRequest> doing in the middle of this code? That doesn't make it very dynamic.
11/20/2009 8:35 PM | Carmel Frank
Gravatar

# re: Dynamic LINQ

You can make it a generic method if required.
11/21/2009 9:31 AM | Sudheer
Gravatar

# re: Dynamic LINQ

Thanks for the example. Wroks like a charm for me!
1/21/2011 5:01 AM | Erik
Gravatar

# re: Dynamic LINQ

I learn so more.
3/31/2011 3:54 AM | led
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: