zoukankan      html  css  js  c++  java
  • The source contains no DataRows

    The source contains no DataRows

    DataTable dt = ds.Tables[4].AsEnumerable()
        .Where(x => ((DateTime)x["EndDate"]).Date >= DateTime.Now.Date)
        .CopyToDataTable();
    

    ds.Tables[4] has rows but it throws the exception

    "The source contains no DataRows."

    Any idea how to handle or get rid of this exception?

    回答

    ds.Tables[4] might, but the result of your linq-query might not, which is likely where the exception is being thrown. Split your method chaining to use interim parameters so you can be dead certain where the error is occurring. It'll also help you check for existing rows before you call CopyToDataTable() and avoid said exception.

    Something like

    DataTable dt = null;
    var rows = ds.Tables[4].AsEnumerable()
        .Where(x => ((DateTime)x["EndDate"]).Date >= DateTime.Now.Date);
    
    if (rows.Any())
        dt = rows.CopyToDataTable();
    

    Another option is to use the ImportRow function on a DataTable

    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);
    
  • 相关阅读:
    常见面试题1
    勒索病毒防范方法
    VMware虚拟机打开后不兼容
    win10桌面显示我的电脑设置
    scala集合和Java集合对应转换操作
    scala中使用redis
    爬虫调研
    hadoop命令
    IDEA打jar包
    spark.mllib
  • 原文地址:https://www.cnblogs.com/chucklu/p/15329235.html
Copyright © 2011-2022 走看看