I have the code:
dt = collListItems.GetDataTable().AsEnumerable()
.Where(a => Convert.ToString(a[\"Expertise\"]).Contains(expertise) && Co
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;
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.
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();
}
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();
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;
}
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();