How to handle CopyToDataTable() when no rows?

后端 未结 6 1809
滥情空心
滥情空心 2020-12-15 03:21

I have the code:

dt = collListItems.GetDataTable().AsEnumerable()
        .Where(a => Convert.ToString(a[\"Expertise\"]).Contains(expertise) && Co         


        
相关标签:
6条回答
  • 2020-12-15 03:55

    You certainly do not want to use a try/catch for this. Try/Catch should be used in truly exceptional circumstances, you do not want to have it drive your control flow. In nearly all situations, there are better methods that are built right into the language/library or require minimal effort to code.

    In this case, you want to capture the table beforehand so that you do not invoke the GetDataTable() method more times than necessary, because we're going to need it if the query does not include any results. You could also optionally include ToList() on the query if the query itself is expensive or long-running, so you only need to do that once.

    Afterwards, it's a matter of testing if there are any rows in the result. If so, you can safely copy to a datatable. Otherwise, just clone the structure of the original table (will not include the rows), so that in either case, you have a proper table structure and can inspect the row count, bind it to a control, etc., and there are no surprises.

    var table = collListItems.GetDataTable();    
    var rows = table.AsEnumerable().Where(...); // optionally include .ToList();
    var dt = rows.Any() ? rows.CopyToDataTable() : table.Clone();
    int filteredCount = dt.Rows.Count;
    
    0 讨论(0)
  • 2020-12-15 03:56

    How about this solution :

                DataRow[] filtered_rows = data.Tables[0].Select(filter_string);
    
                if(filtered_rows.Length > 0)
                {
                    filtered_data = filtered_rows.CopyToDataTable();
                }
                else
                {
                    filtered_data.Clear();
                }
    

    data.Tables[0] is the source table and filtered_data is the resultant table.

    0 讨论(0)
  • 2020-12-15 03:56

    Below code works for me. please try

     DataRow []dataRow = dataTable.Select(query, seq);
     if (dataRow != null && dataRow.Length > 0)
     {
         return dataTable.Select(query, seq).CopyToDataTable();                                            
     }  
    
    0 讨论(0)
  • 2020-12-15 04:01

    you can first judge whether there are rows that match:

    var rows = collListItems.GetDataTable().AsEnumerable()
            .Where(a => Convert.ToString(a["Expertise"]).Contains(expertise) && Convert.ToString(a["Office"]) == office);
    DataTable dt = table.Clone();
    if (rows.Count() > 0)
        dt = rows.CopyToDataTable();
    
    0 讨论(0)
  • 2020-12-15 04:04
    var results = from myRow in dtL1Users.AsEnumerable()
                                              where (Convert.ToInt32(myRow["No_x0020_of_x0020_L1_x0020_Remin"]) >= Convert.ToInt32(maxL1Escalation) && Convert.ToDateTime(myRow["L2_x0020_Last_x0020_Escalated_x0"]) < DateTime.Now.Date.AddDays(-Convert.ToInt32(reminderinterval)))
                                              select myRow;
    
                                foreach (var v in results)
                                {
                                    collEligible = results.CopyToDataTable<DataRow>();
                                    break;
                                }
    
    0 讨论(0)
  • 2020-12-15 04:17

    I think this would be a simpler solution:

    var Adj = (from c in View.AdjustmentsDataSource.AsEnumerable()
                where c["Adjustment"] != System.DBNull.Value
                select c);
    
    if (Adj == null || Adj.Count() == 0)
         return;
    
    DataTable dtChanges = Adj.CopyToDataTable();
    
    0 讨论(0)
提交回复
热议问题