我有一个数据表,如图所示。
让我根据这张图片解释我的要求。我有7行数据。第1行和第2行包含直到UnitSqcNo
的列。我只希望在两者中排第3行。一般情况下,我希望选择具有不同model, unittype, unit
的所有行,并使用更大的CompId
。即表格应该是
答案 0 :(得分:0)
抱歉,我没有VB.Net的经验,所以我在C#中发帖:
似乎您要过滤掉UnitName的双重条目,并仅保留具有最高CompID值的条目。鉴于此,您可以尝试以下方法:
DataTable dt = GetDataTable(); //your logic of building your data table
//order rows by CompID, group by UnitName and keep only first entry of each group
var filteredData = (from row in dt.AsEnumerable() orderby row.Field<int>("CompID") descending group row by row.Field<string>("UnitName")).Select(r => r.First());
DataTable nt = GetDataTable(); //alternatively, you could build a new data table
nt.Rows.Clear(); //clear table as it is used as destination for filtered data
foreach (DataRow r in filteredData) //add filtered rows to table
{
DataRow n = nt.NewRow();
n.ItemArray = r.ItemArray;
nt.Rows.Add(n);
}