使用 Linq 加入动态数据表

发布于 2024-10-21 13:10:11 字数 69 浏览 2 评论 0原文

我有两个数据表,其列是动态的,并且它们都有一个公共列。 现在我想加入这两个表。 以获得合并结果。

提前致谢。

I have two data tables whose columns are dynamic and they both have one common column.
Now I want to join these two tables.
To get combined result.

Thanks in advance.

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

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

发布评论

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

评论(3

前事休说 2024-10-28 13:10:11

您好,您可能希望连接运行时生成的两个数据表。查看此链接。可能对您有用

从两个数据表中查找公共列并将其用于 LINQ 中的联接条件

Hi you might be looking to join the two data tables which are generated on runtime. Check out this link. Might be useful for you

Finding common columns from two datatable and using those for Join condition in LINQ

傲鸠 2024-10-28 13:10:11

一种简单的方法是在表上使用 AsEnumerable() 并将它们连接到公共列数据上。

假设您的表格如下所示:[Table1] -> [ID] [名称] [位置] | [表2]-> [ID] [Description] 和 ID 列具有相同的值


DataTable table1 = new DataTable();
table1.Columns.Add("ID", typeof(int));
table1.Columns.Add("Name", typeof(string));
table1.Columns.Add("Location", typeof(string));
table1.Rows.Add(1, "Name1", "Location1");
table1.Rows.Add(2, "Name2", "Location2");
table1.Rows.Add(3, "Name3", "Location3");

DataTable table2 = new DataTable();
table2.Columns.Add("ID", typeof(int));
table2.Columns.Add("Description", typeof(string));
table2.Rows.Add(1, "Description1");
table2.Rows.Add(2, "Description2");
table2.Rows.Add(3, "Description3");

然后您只需连接 ID 列上的表并选择结果数据集


var joinedTables = from data1 in table1.AsEnumerable()
                   join data2 in table2.AsEnumerable() on data1.Field("ID") equals data2.Field("ID")
                   select new {  id= data1.Field("ID"), 
                     name = data1.Field("Name"), 
                     loc = data1.Field("Location"),
                     desc = data2.Field("Description")
                   }; 

结果数据:

id name  loc       desc
1  Name1 Location1 Description1
2  Name2 Location2 Description2
3  Name3 Location3 Description3

A simple way is to use AsEnumerable() on the tables and to join them on the common column data.

Let's assume that your tables look like this: [Table1] -> [ID] [Name] [Location] | [Table2] -> [ID] [Description] and the ID column has the same values


DataTable table1 = new DataTable();
table1.Columns.Add("ID", typeof(int));
table1.Columns.Add("Name", typeof(string));
table1.Columns.Add("Location", typeof(string));
table1.Rows.Add(1, "Name1", "Location1");
table1.Rows.Add(2, "Name2", "Location2");
table1.Rows.Add(3, "Name3", "Location3");

DataTable table2 = new DataTable();
table2.Columns.Add("ID", typeof(int));
table2.Columns.Add("Description", typeof(string));
table2.Rows.Add(1, "Description1");
table2.Rows.Add(2, "Description2");
table2.Rows.Add(3, "Description3");

Then you just have to join the tables on the ID columns and select the resulting data set


var joinedTables = from data1 in table1.AsEnumerable()
                   join data2 in table2.AsEnumerable() on data1.Field("ID") equals data2.Field("ID")
                   select new {  id= data1.Field("ID"), 
                     name = data1.Field("Name"), 
                     loc = data1.Field("Location"),
                     desc = data2.Field("Description")
                   }; 

The resulting data:

id name  loc       desc
1  Name1 Location1 Description1
2  Name2 Location2 Description2
3  Name3 Location3 Description3
寄居者 2024-10-28 13:10:11

这并不那么容易,如果不使用 ExpandoObject 则更是如此,因为 LINQ 通常经过调整以生成强类型对象,您需要在编译时了解这些对象的架构。我看到这种情况发生的一种方式是通过组合通过转换调用的方法,其中该方法将生成一个动态对象,其中包含所有发现的字段的组合

public ExpandoObject CombineMe(DataRow r1, DataRow r2) 
{
    dynamic x = new ExpandoObject();
    x.ID = r1.Field<int>("ID");
    x.Name = r1.Field<string>("Name");

    // use Expando as dictionary
    IDictionary<String, Object> xd = (IDictionary<String, Object>)x;

    // enumerat both rows for all columns not ID and Name and add to Expando
    foreach (DataColumn c in r1.Table.Columns) 
        if (c.ColumnName != "ID" && c.ColumnName != "Name") 
            xd.Add(c.ColumnName, r1[c]);
    foreach (DataColumn c in r2.Table.Columns)
        if (c.ColumnName != "ID" && c.ColumnName != "Name")
            xd.Add(c.ColumnName, r2[c]);
    return x;
}

/// .... further down

var p = from a in table1.AsEnumerable()
        join b in table2.AsEnumerable() on a.Field<int>("ID") equals b.Field<int>("ID")
        select CombineMe(a, b);

select in linq 并不完全像 SQL select,它只是提供了一个用于转换的占位符,很简单、复杂甚至外部调用代码。

注意:如果您不想使用动态,则可以通过首先枚举两个表中的所有列并创建第三个表来类似地制定解决方案。然后,CombineMe 更改为生成 DataRow 而不是 ExpandoObject,并且最终枚举 p,并将其所有条目(DataRow 的组合实例)添加到结果表。

It's not that easy, more so without using ExpandoObject because LINQ is generally tuned to produce strongly typed objects for which you need to know the schema at compile time. A way I see this happening is through combining method being invoked via transformation, where the method will generate a dynamic object with a combination of all discovered fields

public ExpandoObject CombineMe(DataRow r1, DataRow r2) 
{
    dynamic x = new ExpandoObject();
    x.ID = r1.Field<int>("ID");
    x.Name = r1.Field<string>("Name");

    // use Expando as dictionary
    IDictionary<String, Object> xd = (IDictionary<String, Object>)x;

    // enumerat both rows for all columns not ID and Name and add to Expando
    foreach (DataColumn c in r1.Table.Columns) 
        if (c.ColumnName != "ID" && c.ColumnName != "Name") 
            xd.Add(c.ColumnName, r1[c]);
    foreach (DataColumn c in r2.Table.Columns)
        if (c.ColumnName != "ID" && c.ColumnName != "Name")
            xd.Add(c.ColumnName, r2[c]);
    return x;
}

/// .... further down

var p = from a in table1.AsEnumerable()
        join b in table2.AsEnumerable() on a.Field<int>("ID") equals b.Field<int>("ID")
        select CombineMe(a, b);

select in linq is not exactly like SQL select, it just provides a placeholder for transformation be it simple, complex or even external calling code.

NOTE: If you do not want to use dynamic then a solution can be similarly made by first enumerating all columns in both tables and creating a third table. CombineMe is then changed to generate DataRow instead of ExpandoObject and p is finally enumerated and all its entries (combined instances of DataRow) are added to the resulting table.

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