猿问

在数据表中仅按列显示的正确方法是什么

我的数据表中有以下数据

在上述数据上,我正在使用以下 LINQ 查询过滤那些重复的记录

DataTable HasDuplicates = dt.AsEnumerable()
                              .GroupBy(g => g["Empolyee_CRC"])
                              .Where(c => c.Count() > 1)
                              .Select(g => g.OrderBy(r => r["Empolyee_CRC"]).First())
                              .CopyToDataTable();

哪个返回我下面的数据

http://img4.mukewang.com/62a5a5f400015b8702340075.jpg

以上数据已成功过滤,但我面临的问题是 Budget_CRC 列未删除。所以,为此我写了下面的查询并达到了我想要的结果。


DataTable HasDuplicates = dt.AsEnumerable()

                          .GroupBy(g => g["Empolyee_CRC"])

                          .Where(c => c.Count() > 1)

                          .Select(g => g.OrderBy(r => r["Empolyee_CRC"]).First())

                          .CopyToDataTable();


HasDuplicates.Columns.Remove("Budget_CRC");

HasDuplicates.AcceptChanges();

我的问题是我如何使用上面的 LINQ 查询只获得一个 (Emmplolyee_CRC) 过滤列,而无需编写以下代码行


HasDuplicates.Columns.Remove("Budget_CRC");

HasDuplicates.AcceptChanges();


30秒到达战场
浏览 105回答 2
2回答

胡说叔叔

DataTable如果您希望查询返回,则需要加载另一个DataTable所以使用下面的查询,DataTable dtResult = new DataTable();dtResult.Columns.Add("Empolyee_CRC");DataTable HasDuplicates = dt.AsEnumerable()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .GroupBy(g => g["Empolyee_CRC"])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Where(c => c.Count() > 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .OrderBy(x => x.Key)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(g => dtResult.LoadDataRow(new object[] { g.FirstOrDefault().Field<string>("Empolyee_CRC") }, false))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .CopyToDataTable();或者DataTable HasDuplicates = dt.AsEnumerable()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .GroupBy(g => g["Empolyee_CRC"])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Where(c => c.Count() > 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .OrderBy(x => x.Key)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(g => dtResult.LoadDataRow(new object[] { g.Key }, false))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .CopyToDataTable();输出:编辑:如果您想避免创建新的数据表,那么您可以简单地将您选择的数据投影到列表中。在if下面,您可以使用任何分开的条件,&&也可以按原样使用。取决于你的需要。if (dt.Rows.Count > 0 && dt.AsEnumerable().Select(x => x["Empolyee_CRC"]).Count() > 0){&nbsp; &nbsp; var result = dt.AsEnumerable()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.GroupBy(g => g["Empolyee_CRC"])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Where(c => c.Count() > 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.OrderBy(x => x.Key)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Select(g => new { Empolyee_CRC = g.FirstOrDefault().Field<string>("Empolyee_CRC") })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.ToList();}输出:

jeck猫

您的 LINQ 中似乎有错误的 select 语句,请考虑以下示例:DataTable HasDuplicates = dt.AsEnumerable()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .GroupBy(g => g["Empolyee_CRC"])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Where(c => c.Count() > 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(g => g&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .OrderBy(r => r["Empolyee_CRC"])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(r => r["Empolyee_CRC"])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .First())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .CopyToDataTable();
随时随地看视频慕课网APP
我要回答