zoukankan      html  css  js  c++  java
  • 今天遇到一个问题,linq语句的写法,查询动态字段

      public List<Location> getLocationList(int companyid, string searchValue, string searchField)
    {
    ...
     var dbLocList = from x in _dbLocList
                                where x.company_id == companyid 
                                and   x的searchField==searchValue //这里要查询的字段是变量
                                select x;           
    }
    and那一句怎么实现
     
    为list加一个扩展方法:
    using System.Reflection;

    public static class Extension
    {
    public static List<T> FinData<T>(this List<T> MyDataList, string CName, string CValue)
    {
    BindingFlags bindingFlags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
    PropertyInfo[] propertyInfos = typeof(T).GetProperties(bindingFlags);
    List<T> ResultList = new List<T>();
    foreach (PropertyInfo propertyInfo in propertyInfos)
    {
    if (propertyInfo.Name == CName)
    {
    MyDataList.ForEach(
    j =>
    {
    string re = propertyInfo.GetValue(j, null).ToString();
    if (re == CValue)
    {
    ResultList.Add(j);
    }
    }
    );
    }
    }
    return ResultList;
    }
    }

    上面的问题解决了:

    var dbLocList =Extension.FinData(_dbLocList,searchField,searchValue);

  • 相关阅读:
    JQ选择器
    设计模式
    招银网络面试
    斗鱼面经
    招银科技面经
    用户访问网站基本流程
    shell的条件判断
    crontab -e 和/etc/crontab的区别
    秘钥对登录配置
    CentOS6 x86_64最小化安装优化脚本
  • 原文地址:https://www.cnblogs.com/hiflora/p/3387288.html
Copyright © 2011-2022 走看看