【问题标题】:how to get filtered dataset from Datatable using LINQ如何使用 LINQ 从 Datatable 中获取过滤后的数据集
【发布时间】:2018-06-04 04:29:41
【问题描述】:

我有一个包含员工数据的数据表,此数据绑定到网格视图。我在网格视图中有国家、排名、部分的标题下拉列表。 whenever a selected drop down index changes, i use LINQ to get data as per Say country drop down and save it in Viewstate for further Filtering operations.这很好用。

我将过滤器值保存在字典中,例如 DictionaryFilters,如果没有设置过滤器,则键的值设置为 ALL。

例如,我选择国家,然后选择部分。过滤后的数据很好。但如果我将国家设置为全部,部分过滤器仍然存在。由于我的网格中有 4-5 个过滤下拉列表,如何仅使用一个 LINQ 查询和使用字典值而不是多个条件查询来查询数据表中的数据。是否有任何 % 用于选择列,以便我将字典值中的 ALL 更改为这个特定的运算符?

filter["Country"]="UK"; filter["Section"]="HR"; filter["rank"]="All";



 var query = from a in datatable
                        where a.Field<string>("Country")filter["Country"] &&
                              a.Field<string>("Section")==filter["Section"] &&
                              a.Field<string>("Rank")==filter["rank"] 
                        select a;

【问题讨论】:

    标签: asp.net webforms


    【解决方案1】:

    您是否尝试在过滤前测试您的过滤器值是否设置为“全部”?它会是这样的:

    (filter["Country"] == "All") || (a.Field<string>("Country")filter["Country"])
    

    这样,当filter["Country"] 设置为“All”时,or 运算符不会计算第二条语句(称为短路运算符)。

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2016-01-15
      • 1970-01-01
      • 2019-09-11
      • 2020-12-08
      • 2016-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多