源不包含 DataRows [英] The source contains no DataRows

查看:21
本文介绍了源不包含 DataRows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

ds.Tables[4] 有行但抛出异常

源不包含数据行."

知道如何处理或摆脱这个异常吗?

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

推荐答案

ds.Tables[4] 可能会,但你的 linq-query 的结果可能不会,这可能是异常所在被抛出.拆分您的方法链以使用临时参数,这样您就可以确定发生错误的位置.它还可以帮助您在调用 CopyToDataTable() 之前检查现有行并避免所述异常.

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.

类似的东西

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

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

这篇关于源不包含 DataRows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆