This project has moved. For the latest updates, please go here.


Problem with Where statements and reflection...


When using reflection within the Where method lambda expression it doesn't properly identify the column. Example (commented code works, uncommented code throws an error):
            PropertyInfo field = new Entities.SettlementHistoryTransaction().GetType().GetProperties()
                .Where(x => Attribute.IsDefined(x, typeof(ColumnAttribute), false) && x.Name.ToLower() == "clientid")

            //baseQuery = baseQuery.Where(i => i.ClientID == ClientID);
            baseQuery = baseQuery.Where(i => field.GetValue(i) == ClientID);    
The error when using reflection is as follows:
{"Cannot convert type 'System.Reflection.FieldInfo' to 'System.Reflection.PropertyInfo'"}
The code throwing the exception appears to be here:
        static string ProcessCallExpression(Expression rootExpression, MethodCallExpression e, IDictionary<string, object> parameters, Func<string> getParameterName)
            var method = e.Method;
            string memberName = null;

            if (e.Object is ParameterExpression)
                // This is used by DynamicSQLinqLambdaExpression
                memberName = "{FieldName}";
            else if (e.Object is ConstantExpression)
                throw new Exception("SqlExpressionCompiler.ProcessCallExpresion: ConstantExpression Unsupported");
            } else {
                // Get Column Name to Use
                var member = (PropertyInfo)((dynamic)e.Object).Member;
                memberName = "[" + GetMemberColumnName(member) + "]";

            if (method.DeclaringType == typeof(string))
Closed Oct 19, 2013 at 12:30 AM by crpietschmann
Not An Issue


crpietschmann wrote May 10, 2013 at 12:43 PM

Basically, you are using the "GetValue" method to derive the value to SELECT from the database table, and DynamicSQLinq is unable to derive what COLUMN that value would equate to.

This is an interesting use case, and definitely falls among the extreme edge case uses.

Why don't you do the following in this case:
baseQuery = baseQuery.Where("clientid = @0", ClientID);  

bhillbun wrote May 10, 2013 at 5:22 PM

What a great solution!! Thank you for the assistance! What I've done is developed a custom ORM that uses this SQLinq as its core which enables developers to override the ORM aspect and go straight to ADO.NET if needed and allows for custom transaction control without fear of using DTC. This way you can use a similar system as nHibernate or Entity Framework or cut it out right at the Repository and go straight ADO, etc. I wanted the power of LINQ without being tied to a framework or Linq2SQL.

wrote Oct 19, 2013 at 12:30 AM