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);

  • 相关阅读:
    luoguP3181 [HAOI2016]找相同字符
    luoguP4248 [AHOI2013]差异
    luoguP2852 [USACO06DEC]Milk Patterns
    后缀自动机
    luoguP3975 [TJOI2015]弦论
    luoguP2824 [HEOI2016/TJOI2016]排序(线段树分裂做法)
    组合数学学习笔记
    「题解」:[BZOJ2938]病毒 (AC自动机+dfs)
    Linux新人报到
    指针学习笔记
  • 原文地址:https://www.cnblogs.com/hiflora/p/3387288.html
Copyright © 2011-2022 走看看