将 Linq 查询连接到 DataTable 并将结果连接到 DataView

发布于 2024-12-04 03:05:14 字数 1304 浏览 1 评论 0 原文

我有两个 DataTables 、 Items 及其 Groups 。我有一个 linq 查询,通过连接 2 个表来获取项目名称及其组名称。

    EnumerableRowCollection<DataRow> dvquery = (from item in Items.AsEnumerable()
                                                            join grp in groups.AsEnumerable()
                                                              on item.Field<byte>("Group_ID") equals grp.Field<byte>("ID")
                                                              into item_grp_join
                                                              from itemgrp in item_grp_join
                                                              select new
                                                              {
                                                                  ItemName = (string)led.Field<string>("Name"),
                                                                  GName = (string)itemgrp.Field<string>("Name"),
                                                              });

            DataView dv = dvquery.AsDataView();

但是,我收到编译时错误,因为

无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“System.Data.EnumerableRowCollection”。存在显式转换(您是否缺少强制转换?)

如何解决此错误?我可以轻松地将查询结果转换为列表,但我只需要一个数据视图,这样我就可以将其作为网格的数据源。

任何帮助表示赞赏。 谢谢

I have two DataTables , Items and its Groups. I have a linq query to get item Name and its Group Name by joining the 2 tables.

    EnumerableRowCollection<DataRow> dvquery = (from item in Items.AsEnumerable()
                                                            join grp in groups.AsEnumerable()
                                                              on item.Field<byte>("Group_ID") equals grp.Field<byte>("ID")
                                                              into item_grp_join
                                                              from itemgrp in item_grp_join
                                                              select new
                                                              {
                                                                  ItemName = (string)led.Field<string>("Name"),
                                                                  GName = (string)itemgrp.Field<string>("Name"),
                                                              });

            DataView dv = dvquery.AsDataView();

However I am getting compile time error as

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Data.EnumerableRowCollection'. An explicity conversion exists (are you missing a cast?)

How to solve this error? I can easily convert the query result into a list but I need to have a dataview only , so as I can give it as datasource to grid.

Any help appreciated.
Thanks

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

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

发布评论

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

评论(2

阿楠 2024-12-11 03:05:14

问题是您的查询返回匿名类型的 IEnumerable,并且您不能简单地将其转换为 EnumerableRowCollection

我现在确定是哪个您正在使用的网格类型(例如,来自 winforms?WPF?ASP?ASP MVC?等),但是我希望您实际上应该能够将 IEnumerable 输出传递给它如果您愿意,可以使用 linq 查询 - 例如:

var query = (from item in Items.AsEnumerable()
             join grp in groups.AsEnumerable()
             on item.Field<byte>("Group_ID") equals grp.Field<byte>("ID")
             into item_grp_join
             from itemgrp in item_grp_join
             select new
             {
                ItemName = (string)led.Field<string>("Name"),
                GName = (string)itemgrp.Field<string>("Name"),
             });
grid.DataSource = query; // or possible query.ToList() as you suggest in the question!

如果您确实需要使用 DataView 类型对象,那么有关于如何创建这些对象的博客文章,请尝试:

请注意,如果您希望使用网格进行双向绑定(例如,将更改写回数据库),那么这不太可能“正常工作” - 因为您的 IEnumerable 投影没有绑定回数据源。

The problem is that your query returns an IEnumerable<T> of an anonymous type and you can't simply cast that to a EnumerableRowCollection<DataRow>

I'm now sure which type of Grid you are using (e.g. from winforms? WPF? ASP? ASP MVC? etc), however I would expect that you should actually be able to pass it the IEnumerable output of the linq query if you wanted to - e.g.:

var query = (from item in Items.AsEnumerable()
             join grp in groups.AsEnumerable()
             on item.Field<byte>("Group_ID") equals grp.Field<byte>("ID")
             into item_grp_join
             from itemgrp in item_grp_join
             select new
             {
                ItemName = (string)led.Field<string>("Name"),
                GName = (string)itemgrp.Field<string>("Name"),
             });
grid.DataSource = query; // or possible query.ToList() as you suggest in the question!

If you really need to use a DataView type object, then there are blog posts about how to create these, try:

Note that if you are expecting to use the grid for two-way binding (e.g. for writing changes back to the database), then this is unlikely to "just work" - as your IEnumerable projection isn't tied back to the datasource.

傾旎 2024-12-11 03:05:14

您正在返回匿名对象的列表。最好根据查询结果创建一个数据表。

var query = (from item in Items.AsEnumerable() .......

 DataTable view = new DataTable();
 view.Columns.Add("GroupName");
 view.Columns.Add("ItemName");
 foreach (var t in dvquery)
       {
        view.Rows.Add(t.GName, t.ItemName);
       }
 DataView dv = view.DefaultView;

You are returning an list of anonymous objects. It is better to create a DataTable from your query result.

var query = (from item in Items.AsEnumerable() .......

 DataTable view = new DataTable();
 view.Columns.Add("GroupName");
 view.Columns.Add("ItemName");
 foreach (var t in dvquery)
       {
        view.Rows.Add(t.GName, t.ItemName);
       }
 DataView dv = view.DefaultView;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文