c# - The source contains no DataRows using Linq -


i have multiple data table , filter first data table second data table , same third data table using first data table email.

below c# code:

  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;           }   } 

i getting error in filterededucation , filteredemployee datatable, when there no datarows.

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(); 

the problem come here because trying copytodatatable when don't have records. solution be

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(); } 

Comments