• 今天遇到一个问题,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);

  • 相关阅读:
    善待自己的恻隐之心
    FormLayout and FormData
    jquery获取元素索引值index()方法
    Kohana 之ORM文档篇
    css 圆角相框
    Kohana 之 request
    firefox通过XUL实现textoverflow:ellipsis的效果
    jquery 插件开发备注
    Kohana 之ORM实际使用篇
    PHP扩展编写与编译
  • 原文地址:https://www.cnblogs.com/hiflora/p/3387288.html
Copyright © 2020-2023  润新知