Like in the cycle foreach disablement.



  • There's a code for example:

       foreach (DataRow row1 in ds.Tables[2].Rows)
       {
         if (String.IsNullOrEmpty(row1["clID"].ToString()))
         {
           // какое то действие  
           dict.Add("{Note}", "Примечание");
           break;
         }
         if (!String.IsNullOrEmpty(row1["clID"].ToString()))
         {
           // другое действие  
           dict.Add("{Note}", "Заметка");
           break;
         }
       }  
    

    Result of the procedure:

    | ID | clID |            
    +----+------+            
    | 1  | 1444 |            
    | 2  | 1456 |            
    | 3  | null |
    | 4  | null |  
    

    How to cancel the 2nd. ifif 1st if implemented.
    The problem is, after the first condition, it goes to second and makes a mistake.
    If there's one clID zero in the table, then one if,
    if all clID is not in the table null second condition



  • Because the conditions here are the opposite. ifto design if-else

    if (String.IsNullOrEmpty(row1["clID"].ToString()))
    {
       // какое то действие  
       dict.Add("{Note}", "Примечание");
       break;
    } else {
       // другое действие  
       dict.Add("{Note}", "Заметка");
       break;
    }
    

    It might be worth considering the use of the function. https://msdn.microsoft.com/ru-ru/library/system.convert.tostring(v=vs.110).aspx instead of calling the method ToString♪ If row1 - already. nullIf the first function is used, there will be no error and the second will be an exception.


    Updating: In this case be checked before by, for example, linq: https://msdn.microsoft.com/ru-ru/library/bb337697(v=vs.110).aspx

    if(ds.Tables[2].Rows.AsEnumerable().Any(dr=>String.IsNullOrEmpty(Convert.ToString(row1["clID"])))){
        foreach (DataRow row1 in ds.Tables[2].Rows)
        {
            // какое то действие  
            dict.Add("{Note}", "Примечание");
        }
    }else{
        foreach (DataRow row1 in ds.Tables[2].Rows)
        {
            // другое действие  
            dict.Add("{Note}", "Заметка");
        }
    }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2