如何查询列名与字符串进行比较?

时间:2015-12-30 12:08:47

标签: c# asp.net sql-server asp.net-mvc-4 linq-to-entities

我想在列名称的基础上对列进行排序,这是一个字符串但不幸的是我无法实现这一点,因为OrderByDescending比较了其他内容,但我发送了一个字符串。

public List<DOlead> sortLead(DOuser user, string Item)
    {
        List<DOlead> ObjLead = new List<DOlead>();
        ObjLead = _Context.leads.Where(x => x.is_converted == false).OrderByDescending(Item).ToList();
        return ObjLead;
    }

请帮助我?

4 个答案:

答案 0 :(得分:1)

使用反射

public List<DOlead> sortLead(DOuser user, string Item)
{    
    var propertyInfo = typeof(DOlead).GetProperty(Item);    
    List<DOlead> ObjLead = new List<DOlead>();
    ObjLead = _Context.leads.Where(x => x.is_converted == false).OrderByDescending(x => propertyInfo.GetValue(x, null)).ToList();
    return ObjLead;
}

修改 在得到SO的评论后,经过一些研究,我遇到this answer并决定为用户修改它。

首先必须创建lambda表达式,然后将其传递给order by子句。

创建lambda表达式。

public static class QueryableHelper
{
    public static IQueryable<TModel> OrderBy<TModel>(this IQueryable<TModel> q, string name)
    {
        Type entityType = typeof(TModel);
        PropertyInfo p = entityType.GetProperty(name);
        MethodInfo m = typeof(QueryableHelper).GetMethod("OrderByProperty").MakeGenericMethod(entityType, p.PropertyType);
        return(IQueryable<TModel>) m.Invoke(null, new object[] { q, p });
    }

    public static IQueryable<TModel> OrderByDescending<TModel>(this IQueryable<TModel> q, string name)
    {
        Type entityType = typeof(TModel);
        PropertyInfo p = entityType.GetProperty(name);
        MethodInfo m = typeof(QueryableHelper).GetMethod("OrderByPropertyDescending").MakeGenericMethod(entityType, p.PropertyType);
        return (IQueryable<TModel>)m.Invoke(null, new object[] { q, p });
    }

    public static IQueryable<TModel> OrderByPropertyDescending<TModel, TRet>(IQueryable<TModel> q, PropertyInfo p)
    {
        ParameterExpression pe = Expression.Parameter(typeof(TModel));
        Expression se = Expression.Convert(Expression.Property(pe, p), typeof(object));
        return q.OrderByDescending(Expression.Lambda<Func<TModel, TRet>>(se, pe));
    }

    public static IQueryable<TModel> OrderByProperty<TModel, TRet>(IQueryable<TModel> q, PropertyInfo p)
    {
        ParameterExpression pe = Expression.Parameter(typeof(TModel));
        Expression se = Expression.Convert(Expression.Property(pe, p), typeof(object));
        return q.OrderBy(Expression.Lambda<Func<TModel, TRet>>(se, pe));
    }
}

您修改过的方法

public List<DOlead> sortLead(DOuser user, string Item)
    {      
        List<DOlead> ObjLead = new List<DOlead>();
        ObjLead = _Context.leads.Where(x => x.is_converted == false).OrderByDescending(Item).ToList();
        return ObjLead;
    }

答案 1 :(得分:0)

从Stack,我想你可以使用反射:

from x in db.TableName
    where (x.GetType().GetProperty(stringCOLUMN_1_or2).GetValue(x, null)) == " 8"
select x;

不确定是否有任何简单的Linqish方法可以做到这一点...

Assuming dynamic Linq will work it would just be:

from x in objets
.Where(stringCOLUMN_1_or2  + " = ' " + 8 + "'")
select x

此处有关于动态Linq with SQL的更多信息: http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

答案 2 :(得分:0)

您应该使用表达式构建器通过字符串属性简单地实现此示例:

public class OrderByData
    {
        public string PropertyName { get; set; }
    }
    public static class ExpressionBuilder
    {
        public static Expression<Func<T, string>> GetExpression<T>(OrderByData filter)
        {

            ParameterExpression param = Expression.Parameter(typeof(T), "t");
            Expression exp = GetExpression<T>(param, filter);
            return Expression.Lambda<Func<T, string>>(exp, param);
        }

        private static Expression GetExpression<T>(ParameterExpression param, OrderByData filter)
        {
            MemberExpression member = Expression.Property(param, filter.PropertyName);
            return member;
        }
    }

然后像这样打电话:

    public List<DOlead> sortLead(DOuser user, string Item)
        {
            List<DOlead> ObjLead = new List<DOlead>();
 new OrderByData { PropertyName = Item };
            var deleg = ExpressionBuilder.GetExpression<lead>(filter).Compile();
            ObjLead = _Context.leads.Where(x => x.is_converted == false).OrderByDescending(deleg).ToList();
            return ObjLead;
        }

你也可以扩展到不仅使用其他类型的字符串。

答案 3 :(得分:0)

您需要将委托传递给OrderByDescending方法。 由于您只需拥有属性名称,因此需要动态访问该属性,因此可以使用动态表达式来执行此操作:

public List<DOlead> sortLead(DOuser user, string Item)
    {
        List<DOlead> ObjLead = new List<DOlead>();
        ObjLead = _Context.leads.Where(x => x.is_converted == false).OrderByDescending((d) =>
                 Expression.Lambda(Expression.Property(Expression.Constant(d), Item)).Compile()()).ToList();
        return ObjLead;
    }