.net => linq to entity => select 动态字段名怎么写

2025-04-28 07:59:47
推荐回答(3个)
回答1:

  1. 使用Expression来构造,学习周期比较长

  2. 使用附件里面微软的开源类库Dynamic类,附件就是示例

  3. 不懂可以留联系。


回答2:

  public List GetSortedEmployee(string sortField, bool isAsc)
        {
            List emp = GetEmployee();
            if (isAsc)
            {
                var res = from e in emp orderby GetPropertyValue(e, sortField) select e;
                emp = res.ToList();
            }
            else
            {
                var res = from e in emp orderby GetPropertyValue(e, sortField) descending select e;
                emp = res.ToList();
            }
            return emp;
        }
        private object GetPropertyValue(object obj, string property)
        {
            System.Reflection.PropertyInfo propertyInfo = obj.GetType().GetProperty(property);
            return propertyInfo.GetValue(obj, null);
        }
        
        不过这样只能把数据全读出来在筛选,就是在内存中对list操作

回答3:

我不知道。