我有多个数据表并使用第一个数据表电子邮件过滤第一个数据表和第二个数据表,并使用第一个数据表电子邮件与第三个数据表相同。
以下是我的C#代码:
public void getexport()
{
DateTime Fromdate = DateTime.ParseExact(txt_FromDate.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture);
DateTime Todate = DateTime.ParseExact(txt_ToDate.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture);
bo.Dateused = Fromdate;
bo.Dateused2 = Todate;
var ds = new DataSet();
var dt = new DataTable("Registration Details");
DataTable dt1 = new DataTable("Education Details");
DataTable dt2 = new DataTable("Employeement Details");
dt = bl.Get_Registrationdetailsbydate(bo);
gv_Regdetails.DataSource = dt;
gv_Regdetails.DataBind();
dt1 = bl.Get_Educationdetailsbydate(bo);
dt2 = bl.Get_Employmentdetailsbydate(bo);
Session["Fromdate"] = txt_FromDate.Text;
Session["Todate"] = txt_ToDate.Text;
if (gv_Regdetails.Rows.Count > 0)
{
DataTable filteredEducation = dt1.AsEnumerable()
.Where(x => dt.AsEnumerable()
.Any(z => z.Field<string>("Email").Trim() == x.Field<string>("Email").Trim())).CopyToDataTable();
DataTable filteredEmployee = dt2.AsEnumerable()
.Where(x => dt.AsEnumerable()
.Any(z => z.Field<string>("Email").Trim() == x.Field<string> ("Email").Trim())).CopyToDataTable();
dt.TableName = "Registration Details";
filteredEducation.TableName = "Education Details";
filteredEmployee.TableName = "Employeement Details";
ds.Tables.Add(dt);
ds.Tables.Add(filteredEducation);
ds.Tables.Add(filteredEmployee);
ExcelHelper.ToExcel(ds, "DangoteUsers.xls", Page.Response);
btnExport.Visible = true;
}
}
当没有DataRows时,我在filteredEducation和filteredEmployee Datatable中收到错误。
DataTable filteredEducation = dt1.AsEnumerable()
.Where(x => dt.AsEnumerable()
.Any(z => z.Field<string>("Email").Trim() == x.Field<string>("Email").Trim())).CopyToDataTable();
DataTable filteredEmployee = dt2.AsEnumerable()
.Where(x => dt.AsEnumerable()
.Any(z => z.Field<string>("Email").Trim() == x.Field<string>("Email").Trim())).CopyToDataTable();
答案 0 :(得分:2)
问题来自这里,因为当你没有任何记录时,你正在尝试CopyToDataTable。所以解决方案将是
var firstDataTable = dt1.AsEnumerable()
.Where(x => dt.AsEnumerable()
.Any(z => z.Field<string>("Email").Trim() == x.Field<string>("Email").Trim()));
DataTable filteredEducation = new DataTable();
if(firstDataTable.Any())
{
filteredEducation = firstDataTable.CopyToDataTable();
}
答案 1 :(得分:1)
var row = dt1.AsEnumerable()
.Where(x => dt.AsEnumerable()
.Any(z => z.Field<string>("Email").Trim() == x.Field<string>("Email").Trim())).ToList();//Add ToList() at the end of this query.
DataTable filteredEducation = row.Any() ? row.CopyToDataTable() : filteredEducation.Clone();