为什么我从这个声明中得到 Null。 C# 中的查询语法

发布于 2024-09-03 04:21:12 字数 814 浏览 1 评论 0 原文

这不起作用。将 Null 返回到 dept_list。

        var dept_list = ((from map in DtMapGuestDepartment.AsEnumerable()
                         where map.Field<Nullable<long>>("Guest_Id") == 174
                         select map.Field<Nullable<long>>("Department_id")).Distinct())as IEnumerable<DataRow>;
       DataTable dt = dept_list.CopyToDataTable(); //dept_list comes null here

这按预期工作。

        var dept_list = from map in DtMapGuestDepartment.AsEnumerable()
                         where map.Field<Nullable<long>>("Guest_Id") == 174
                         select map;
       DataTable dt = dept_list.CopyToDataTable(); //when used like this runs correct.

我在这里犯了什么错误。 ?

This is not working. Returns Null to dept_list.

        var dept_list = ((from map in DtMapGuestDepartment.AsEnumerable()
                         where map.Field<Nullable<long>>("Guest_Id") == 174
                         select map.Field<Nullable<long>>("Department_id")).Distinct())as IEnumerable<DataRow>;
       DataTable dt = dept_list.CopyToDataTable(); //dept_list comes null here

This works as desired.

        var dept_list = from map in DtMapGuestDepartment.AsEnumerable()
                         where map.Field<Nullable<long>>("Guest_Id") == 174
                         select map;
       DataTable dt = dept_list.CopyToDataTable(); //when used like this runs correct.

What mistake is being done by me here. ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稀香 2024-09-10 04:21:12

您的第一个查询返回可枚举的(部门 ID),而不是可枚举的数据行(如第二个查询中所示)。

由于 IEnumerable> 不是 IEnumerable 的子类型,因此 as 运算符返回 null。

(作为旁注,使用普通转换而不是 as 会给你一个 InvalidCastException,它比仅仅返回 null 更有助于查找错误。)


编辑:如果你真的需要一个 DataTable最后,我想你将不得不手动构建它(未经测试):

var dept_list = ((from map in DtMapGuestDepartment.AsEnumerable()
                     where map.Field<Nullable<long>>("Guest_Id") == 174
                     select map.Field<Nullable<long>>("Department_id")).Distinct())

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Department_id", typeof(long?)));
foreach (long? dept in dept_list) {
    dt.Rows.Add(dept);
}

Your first query is returning an enumerable of values (of the department IDs) instead of an enumerable of data rows (as in the second query).

Since IEnumerable<Nullable<long>> is not a subtype of IEnumerable<DataRow>, the as operator returns null.

(As a side note, using a normal cast instead of as would have given you an InvalidCastException, which is more helpful in finding errors than just returning null.)


EDIT: If you really need a DataTable in the end, I guess you will have to construct it manually (untested):

var dept_list = ((from map in DtMapGuestDepartment.AsEnumerable()
                     where map.Field<Nullable<long>>("Guest_Id") == 174
                     select map.Field<Nullable<long>>("Department_id")).Distinct())

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Department_id", typeof(long?)));
foreach (long? dept in dept_list) {
    dt.Rows.Add(dept);
}
乖乖 2024-09-10 04:21:12

可能失败的是 as IEnumerable 的转换。如果 T 无法转换为 U,则表达式 foo as U 将为任何 返回 null T foo。看起来第一个 LINQ 语句(直到 as 表达式)的结果实际上是一个 IEnumerable

第二条语句有效,因为您让类型推断为您完成工作。

It's the cast as IEnumerable<DataRow> that is probably failing. If T is not convertible to U, then the expression foo as U will return null for any T foo. It looks like the result of the first LINQ statement (up to the as expression) is actually an IEnumerable<long?>.

The second statement works since you're letting type inference do the work for you.

无声情话 2024-09-10 04:21:12

在第一个示例中,您选择 map.Field>("Department_id")) 因此其返回值不是 IEnumerable

in the first example you select map.Field>("Department_id")) so its return value is not IEnumerable

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文