源不包含数据行
The source contains no DataRows
DataTable dt = ds.Tables[4].AsEnumerable()
.Where(x => ((DateTime)x["EndDate"]).Date >= DateTime.Now.Date)
.CopyToDataTable();
ds.Tables[4]
有行但抛出异常
"The source contains no DataRows."
知道如何处理或消除此异常吗?
ds.Tables[4]
可能,但您的 linq 查询的结果可能不会,这很可能是抛出异常的地方。拆分您的方法链接以使用临时参数,这样您就可以确定错误发生的位置。它还会帮助您在调用 CopyToDataTable()
之前检查现有行,并 避免 所述异常。
类似
DataTable dt = null;
var rows = ds.Tables[4].AsEnumerable()
.Where(x => ((DateTime)x["EndDate"]).Date >= DateTime.Now.Date);
if (rows.Any())
dt = rows.CopyToDataTable();
另一种选择是在 DataTable
上使用 ImportRow
函数
DataTable dt = ds.Tables[4].Clone();
var rows = ds.Tables[4].AsEnumerable()
.Where(x => ((DateTime)x["EndDate"]).Date >= DateTime.Now.Date);
foreach (var row in rows)
dt.ImportRow(row);
简单地分成两行
var rowSources = ds.Tables[4].AsEnumerable()
.Where(x => ((DateTime)x["EndDate"]).Date >= DateTime.Now.Date);
if(rowSources.Any())
{
DataTable dt = rowSources.CopyToDataTable();
... code that deals with the datatable object
}
else
{
... error message ?
}
这允许检查结果是否包含任何 DataRow,如果是,那么您可以调用 CopyToDataTable 方法。
DataTable dt = ds.Tables[4].AsEnumerable()
.Where(x => ((DateTime)x["EndDate"]).Date >= DateTime.Now.Date)
.CopyToDataTable();
ds.Tables[4]
有行但抛出异常
"The source contains no DataRows."
知道如何处理或消除此异常吗?
ds.Tables[4]
可能,但您的 linq 查询的结果可能不会,这很可能是抛出异常的地方。拆分您的方法链接以使用临时参数,这样您就可以确定错误发生的位置。它还会帮助您在调用 CopyToDataTable()
之前检查现有行,并 避免 所述异常。
类似
DataTable dt = null;
var rows = ds.Tables[4].AsEnumerable()
.Where(x => ((DateTime)x["EndDate"]).Date >= DateTime.Now.Date);
if (rows.Any())
dt = rows.CopyToDataTable();
另一种选择是在 DataTable
ImportRow
函数
DataTable dt = ds.Tables[4].Clone();
var rows = ds.Tables[4].AsEnumerable()
.Where(x => ((DateTime)x["EndDate"]).Date >= DateTime.Now.Date);
foreach (var row in rows)
dt.ImportRow(row);
简单地分成两行
var rowSources = ds.Tables[4].AsEnumerable()
.Where(x => ((DateTime)x["EndDate"]).Date >= DateTime.Now.Date);
if(rowSources.Any())
{
DataTable dt = rowSources.CopyToDataTable();
... code that deals with the datatable object
}
else
{
... error message ?
}
这允许检查结果是否包含任何 DataRow,如果是,那么您可以调用 CopyToDataTable 方法。