c# - How I get the index of a DataTable Row with two calues of two other Column Values? -


to application:

i have 2 datatables , want filter out first datatable second. columns user , modul first datatable , if not exist in second add new row.

this structure second datatable:

user | modul | time | department | status  

and want check 2 columns (user , modul) whether row values in second datatable exist. if entry exist need row index. how can linq? name of second datatable analyse_table.

here code:

private static datatable filterdatatable(datatable nofilter_datatable)          {             datatable analyse_table = new datatable("filter_analyse");              datacolumn user = new datacolumn("user", typeof(string));             datacolumn modul = new datacolumn("modul", typeof(string));             datacolumn time = new datacolumn("time", typeof(string));             datacolumn department = new datacolumn("department", typeof(string));             datacolumn status = new datacolumn("status", typeof(string));              analyse_table.columns.add(user);             analyse_table.columns.add(modul);             analyse_table.columns.add(time);             analyse_table.columns.add(department);             analyse_table.columns.add(status);              foreach (datarow nf_row in nofilter_datatable.rows)             {                 string user = nf_row["user"].tostring();                 string modul = nf_row["modul"].tostring();                  string out = nf_row["out"].tostring();                 string in = nf_row["in"].tostring();                  bool contains_user = analyse_table.asenumerable()                     .any(row => user == row.field<string>("user"));                  bool contains_modul = analyse_table.asenumerable()                     .any(row => modul == row.field<string>("modul"));                  if (!contains_user || !contains_modul)                 {                     try                     {                         datarow row = analyse_table.newrow();                          row["user"] = user;                         row["modul"] = modul;                          if (out != string.empty)                         {                             row["time"] = out;                             row["status"] = "out";                         }                         else if (in != string.empty)                         {                             row["time"] = in;                             row["status"] = "in";                         }                          string[] userspli = user.split('@');                          row["department"] = getactivedirectoryattribute(userspli[0], "department", domaincontroller);                          analyse_table.rows.add(row);                     }                     catch (exception)                     {                      }                  }                  if (contains_user && contains_modul)                 {                     //index??                      //string status = analyse_table.rows[0]["status"].tostring();                  }               }              return analyse_table;          } 

i need help.

there no need know index in analyse_table, using firstordefault should allow find directly row required

var rowuser = analyse_table.asenumerable()                       .firstordefault(row => user == row.field<string>("user"));  var rowmodul = analyse_table.asenumerable()                 .firstordefault(row => modul == row.field<string>("modul"));  if (rowuser == null || rowmodul == null) {     // not exist add new row  } if (rowuser != null && rowmodul != null) {      string statususer = rowuser["status"].tostring();      string statusmodul = rowmodul["status"].tostring();  } 

however, having executed 2 different queries search rows, have no guarantees 2 rows same. perhaps need change code search both user , modul in same row

var rowresult = analyse_table.asenumerable()                 .firstordefault(row => (user == row.field<string>("user") &&                                          modul == row.field<string>("modul"));  if(rowresult == null)     // add new else     // read status 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -