簡體   English   中英

如何以另一個列為條件從DataTable對象中選擇不同的列組合?

[英]How can I select distinct column combinations from a DataTable object with another column as a condition?

我正在使用C#2010。

我有一個正在使用的DataTable對象,該對象具有以下結構(並填充有示例數據):

"name"    "ID"    "hiredate"    "termdate"
Bobby     1        5/1/2011       7/1/2011
Peggy     2        5/1/2011
Jenny     3        5/2/2011
Jenny     3        5/2/2013
Jenny     3        5/2/2011       6/1/2011
Peggy     2        5/1/2011

我想過濾此DataTable以僅保留不同的(“ ID”,“ hiredate”)組合。 此外,如您所見,並非每個人都有“ termdate”值。 在決定保留哪個不同的(“ ID”,“ hiredate”)組合時,我要保留一個也具有“ termdate”的組合。 如果它們中都沒有“ termdate”,則丟棄哪個是無關緊要的。

因此,執行此操作后的結果表將是:

"name"    "ID"    "hiredate"    "termdate"
Bobby     1        5/1/2011       7/1/2011
Peggy     2        5/1/2011
Jenny     3        5/2/2013
Jenny     3        5/2/2011       6/1/2011

珍妮有兩個條目,因為她出現了兩個不同的“受雇”值,並且其中一個也被復制了-刪除了沒有“任期”的條目。

關於如何在C#中執行此操作的任何建議? 同樣,我正在使用DataTable對象。 我仍然需要保留“ name”和“ termdate”字段-如果沒有,那么我可以獲得一個不同的(“ ID”,“ hiredate”)列表,但確實需要保留它們。

謝謝你的幫助!

我可能會做這樣的事情,dt是您的原始數據表-

由於數據視圖的排序,這應該涵蓋所有可能的情況,保留的日期列表允許為用戶添加多個雇用日期。

            DataView dv = new DataView(dt);
            dv.Sort = "ID ASC, HireDate DESC, TermDate DESC";

            string lastID = "0";
            List<DateTime> addedHireDatesForUser = new List<DateTime>();

            foreach (DataRowView drv in dv)
            {
                if (drv["ID"].ToString() != lastID)
                {
                    addedHireDatesForUser = new List<DateTime>();
                    addedHireDatesForUser.Add(DateTime.Parse(drv["HireDate"].ToString()));

                    // NEXT ID, ADD ROW TO NEW DATATABLE
                }
                else if (!addedHireDatesForUser.Contains(DateTime.Parse(drv["HireDate"].ToString())))
                {
                    addedHireDatesForUser.Add(DateTime.Parse(drv["HireDate"].ToString());

                    // NEXT DATE, ADD ROW TO NEW DATATABLE
                }

                lastID = drv["ID"].ToString();
            }

這是偽代碼,未經測試,但這可能會讓您入門。

       //Get rows with distinct name, ID hiredate
        DataView dview = new DataView(table);
        DataTable distinctValues = dview.ToTable(true, "name", "ID", "hiredate");

        //Get rows with termdate not null
        DataView tview = new DataView(table, "Isnull(termdate,'') <> ''", "ID", DataViewRowState.CurrentRows);
        DataTable termValues = tview.ToTable(true, "name", "ID", "hiredate", "termdate");

        DataTable result = new DataTable("Table");
        foreach (DataRow r in distinctValues.Rows)
        {
            DataRow[] compare = termValues.Select(string.Format("name='{0}' AND ID='{1}' AND hiredate='{2}'", r["name"], r["ID"], r["hiredate"]));
            //If a value exists with a termdate use it
            if (compare != null && compare.Length > 0)
            {
                DataRow row = result.NewRow();
                row["name"] = compare[0]["name"];
                row["ID"] = compare[0]["ID"];
                row["hiredate"] = compare[0]["hiredate"];
                row["termdate"] = compare[0]["termdate"];
                result.Rows.Add(row);
            }
            // Otherwise use the distinct value
            else
            {
                DataRow row = result.NewRow();
                row["name"] = r["name"];
                row["ID"] = r["ID"];
                row["hiredate"] = r["hiredate"];
                result.Rows.Add(row);
            }
        }

要從數據表中獲取不同的值列表...

DataView dv = new DataView(dt);
DataTable dtResults = dv.ToTable(true, "name", "ID", "hiredate");

然后放置termdates ...

dtResults.Columns.Add("termdate", typeof(DateTime));
foreach (DataRow dr in from x in dt.Rows.Cast<DataRow>() where !x.IsNull("termdate") select x) {
    DataRow drResult = dtResults.Select(string.Format("name=\'{0}\' and ID=\'{1}\' and hiredate=\'{2}\'", dr["name"], dr["ID"], dr["hiredate"])).Single();
    drResult["termdate"] = dr["termdate"];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM