【问题标题】:Linq Datatable subset using group by and having count >1Linq Datatable 子集使用 group by 并且计数 >1
【发布时间】:2017-02-14 19:12:55
【问题描述】:

在 Linq 方面需要一些帮助。

我有一个如下所示的数据表 I just have the W_text with me to search

使用上面的数据表,我想提取具有以下结果的行子集

The key to search is "First Employee"

如您所见,我想获取其中 W_Text 值为“First”和“Employee”但具有相同 l_id 的行的子集。

在数据库术语中,这将是一个 Select * from Table 1 where W_Text in ('First','Employee') group by l_id with l_id > 1(或类似的东西)。

如何在 C# 中使用数据表实现这一点?

我尝试使用以下代码,但这也为我提供了包含其他行的整个数据表。我不确定如何使用 group by 和 having 子句。如果有人可以在这方面帮助我,我会很高兴。

var results = from myRow in dtResult.AsEnumerable()
              where myRow.Field<string>("W_Text") == "First" ||
                    myRow.Field<string>("W_Text") == "Employee"
              select myRow;

dtCopy = results.CopyToDataTable();

【问题讨论】:

    标签: c# linq datatable


    【解决方案1】:

    检查以下内容:

          List<string> wTextFilter = new List<string>();
            wTextFilter.Add("First");
            wTextFilter.Add("Employee");
    
            // Get all Id's that satisfy all conditions:            
            List<int> results = dt.AsEnumerable()
                // Get all Id's:
                .Select(dataRow => dataRow.Field<int>("l_id"))
                // Filter the Id's : 
                .Where(id =>
                    // the Id should be greater than one.
                        id > 1   &&
                        // check if all W_Text entries has a record in the datatable with the same Id.
                        wTextFilter.All(W_Text => dt.AsEnumerable().Any(dataRow => dataRow.Field<string>("W_Text") == W_Text && dataRow.Field<int>("l_id") == id)))
                        .Distinct().ToList();
    
            // Get all datatable rows filtered by the list of Id's.
            DataTable dtCopy = dt.AsEnumerable().Where(dataRow => results.Contains((dataRow.Field<int>("l_id")))).CopyToDataTable();
    

    【讨论】:

    • 谢谢阿布杜拉。这也给了我一个包含所有“First”和“Employee”出现的数据集。我只想要一个数据集,其中“First”和“Employee”都存在相同的 l_id,换句话说,count (l_id) >1
    • 新编辑后检查上面显示的代码sn-p。
    • 工作出色!非常感谢阿卜杜拉。非常感谢您的回复。你能帮我理解代码是如何工作的吗?我很难理解 Linq,尤其是这些聚合函数。再次感谢!
    • 不客气 :)。我在上面的代码中添加了一些 cmets。
    • 有一个后续问题先生 - 是否可以动态构建此查询?例如,这里我的输入字符串是“First Employee”,所以查询有“First”和“Employee”。假设明天我有一个名为“First Employee Salary”的东西,那么我需要搜索所有 3 个单词。我尝试过动态追加,但它失败了。任何帮助将不胜感激。提前感谢您的帮助。
    【解决方案2】:

    你好,你可以像这样使用

    var results = (from myRow in dtResult.AsEnumerable()
                  where myRow.Field<string>("W_Text") == "First" ||
                  myRow.Field<string>("W_Text") == "Employee"
                  select myRow ).ToLookup(item => item.Field<string>("l_id"));
    

    或

    var results = (from myRow in dtResult.AsEnumerable()
                   where myRow.Field<string>("W_Text") == "First" ||
                   myRow.Field<string>("W_Text") == "Employee"
                   select myRow).GroupBy(item => item.Field<string>("l_id")).ToList();
    

    【讨论】:

    • 感谢您的快速回复瓦拉。虽然这现在显示结果计数与子集相同,但我无法复制到 Datatable。你能告诉我如何遍历结果变量吗?我们说话时我也在抬头,但我们将非常感谢您的帮助。
    • 抱歉耽搁了,请查看此链接stackoverflow.com/questions/24566246/…
    • 再次感谢 Vara,但您的回复是返回所有包含文本“First”或“Employee”的 l_id 值。我只想要包含“First”和“Employee”的唯一 l_id。再次感谢
    猜你喜欢
    • 1970-01-01
    • 2012-08-29
    • 2016-12-04
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多