String condition in List<T>
<p>I have a class named <code>HomeInfo</code></p>

<pre><code>public class HomeInfo
{
   public int ID {get;set;}
   public string OwnerName {get;set;}
   public string Address {get;set;}
   public int EstimatedValue {get;set;}
}
</code></pre>

<p>I get data from server and i add that into <code>List&lt;HomeInfo&gt; listHomeInfo</code></p>

<p>Now in my GUI I need to allow filtering results based on user input, so my client wants a textbox for Estimated Value and he wants to enter text there like '>30k and &lt;50k' or '>50k', I parsed and converted these values and created object of class</p>

<pre><code>public class ExpressionValue
{
    public float? FirstDigit { get; set; }
    /// &lt;summary&gt;
    /// &gt;, &gt;=, &lt;,&lt;=
    /// &lt;/summary&gt;
    public string FirstExpCondition { get; set; }
    /// &lt;summary&gt;
    /// OR, AND
    /// &lt;/summary&gt;
    public string ConditionOperator { get; set; }
    public float SecondDigit { get; set; }
    public string SecondExpCondition { get; set; }
}
</code></pre>

<p>Using an ExpressionValue object I am able to create a proper condition string.
Now I am able to create a condition string like 'EstimatedValue > 30000 AND EstimatedValue &lt; 60000' or 'EstimatedValue &lt; 50000'</p>

<p>I don't know how can I effectively apply this condition on 'List listHomeInfo' since as far i know <code>List&lt;T&gt;.Where()</code> doesn't support string condition. I know a way around it is to convert the list to DataTable and use <code>Select(string expression)</code> method and then convert <code>DataRow[]</code> to <code>List&lt;HomeInfo&gt;</code>, but I think there may be a better way to achieve this.</p>

<p>[EDIT]</p>

<p>I created two methods to help me out but i am getting exception "The binary operator GreaterThan is not defined for the types 'System.Single' and 'System.Double'." when creating BinaryExpression.</p>

<pre><code>public static Expression&lt;Func&lt;T, bool&gt;&gt; ParseExpressionCondition&lt;T&gt;(string expression, string fieldName)
    {
        try
        {
            string decimalNumRegex = @"\d+(\.\d{1,2})?";
            List&lt;string&gt; matchPatterns = new List&lt;string&gt;() { "&gt;=", "&gt;", "&lt;=", "&lt;" };
            ExpressionValue expValue = new ExpressionValue();
            Dictionary&lt;string, string&gt; conditions = new Dictionary&lt;string, string&gt;();
            var parameter = Expression.Parameter(typeof(T), typeof(T).ToString());
            //var lhs = Expression.GreaterThan(Expression.Property(parameter, "EstimatedValue"), Expression.Constant(30000));
            BinaryExpression lhs = null, rhs = null;
            object objectValue = null;
            string condOperator = null;
            foreach (string pattern in matchPatterns)
            {
                Match match = Regex.Match(expression, pattern + decimalNumRegex);

                if (match.Success)
                {
                    //get digit part
                    double digit = double.Parse(Regex.Match(match.Value, decimalNumRegex).Value);
                    if (!expValue.FirstDigit.HasValue)
                    {
                        objectValue = digit;
                        condOperator = match.Value.Replace(digit.ToString(), "");
                        lhs = GetBinaryExpression(parameter, fieldName, objectValue, condOperator);
                    }
                    else
                    {
                        objectValue = digit;
                        condOperator = match.Value.Replace(digit.ToString(), "");
                        rhs = GetBinaryExpression(parameter, fieldName, objectValue, condOperator);
                    }
                }
            }

            if (expression.ToLower().Contains("and"))
                return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(Expression.And(lhs, rhs), parameter);
            else if (expression.ToLower().Contains("or"))
                return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(Expression.Or(lhs, rhs), parameter);


            return null;
        }
        catch (Exception ex)
        {
            Logger.WriteLog(ex);
            throw ex;
        }
    }

    private static BinaryExpression GetBinaryExpression(ParameterExpression paraExp, string fieldName, object expressionValue, string conditionOperator)
    {
        try
        {
            BinaryExpression binExp = null;
            MemberExpression expressionLeft = Expression.Property(paraExp, fieldName);
            Expression expressionRight = Expression.Constant(expressionValue );
            switch (conditionOperator)
            {
                case "&gt;":
                    binExp = Expression.GreaterThan(expressionLeft, expressionRight);
                    break;
                case "&gt;=":
                    binExp = Expression.GreaterThanOrEqual(expressionLeft, expressionRight);
                    break;
                case "&lt;":
                    binExp = Expression.LessThan(expressionLeft, expressionRight);
                    break;
                case "&lt;=":
                    binExp = Expression.LessThanOrEqual(expressionLeft, expressionRight);
                    break;
            }
            return binExp;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
</code></pre>
