How do I find a row in the datatable, and verified that the 'ID' is unique, but exclude the row that is being verified?
The Datatable in the code pulls all the rows, and it is finding all the rows for a duplicate ID. But I need it to exclude the row being verifity for that ID. How can I exclude it?
if (ID.Text != Dataset.Tables[0].Rows[0]["ID"].ToString())
{
string ID = "";
ID = ID.Text.ToString();
DataRow[] findrow = Datatable.Select("ID = '" + ID + "' ");
string findID = "";
foreach (DataRow row in findrow)
{
findID = row["ID"].ToString();
}
if (findID.ToString() == ID.ToString())
{
divError.Style.Add("visibility", "visible");
divError.Style.Add("display", "");
divError.Style.Add("top", "120%");
divError.Style.Add("left", "50%");
divError.Style.Add("margin-top", "-9em");
divError.Style.Add("margin-left", "-12em");
}
}
1. If its not possible to get distinct records, then get a new column which will have distinct value for each row e.g. autoincrement. This you can achieve in DB query or at code level.
2. Get list of duplicates using LINQ and attached distinct value using min/max;
3. Use LINQ Outer join with step 2 output using distinct value as JOIN column
沒有留言:
張貼留言