ado.net 将两行合并为 1 行

发布于 2024-09-24 08:25:13 字数 989 浏览 0 评论 0原文

我正在开发一个项目,该项目将结合几个共享点列表项集合。它基本上是将三个共享点列表放在一起。

我已经获得了三个共享点查询来返回正确的结果,并且我使用 .GetDataTable() 方法来生成用于 LINQ 连接的对象。

下面是我的代码。我可能会错误地处理这个问题,但如果有一种方法可以返回一个包含所有三个对象的数据行,那就是我所需要的。

DataTable dtContracts = _contractList.GetDataTable();
        DataTable dtCustomer = _customerList.GetDataTable();

        var joined = from contracts in dtContracts.AsEnumerable()
                           join customers in dtCustomer.AsEnumerable()
                           on contracts.Field<String>("ContractNumber")
                           equals customers.Field<String>("ContractNumber")
                           join SPListItem loans in _loanList
                           on contracts["ContractNumber"] equals loans["Contract_x0020_Number"] 
                           into JoinedContractLoans
                           from loans in JoinedContractLoans.DefaultIfEmpty()
                           select new { contracts, customers,loans };

I'm working on a project that will combine a couple of sharepoint listitemcollections. It's basically to bring together three sharepoint lists.

I've already gotten my three sharepoint queries to return my proper results and im using the .GetDataTable() method to generate my objects for LINQ joins.

Below is my code. I may be approaching this wrong but if there is a way I can return one data row that has all three objects combined is all I need.

DataTable dtContracts = _contractList.GetDataTable();
        DataTable dtCustomer = _customerList.GetDataTable();

        var joined = from contracts in dtContracts.AsEnumerable()
                           join customers in dtCustomer.AsEnumerable()
                           on contracts.Field<String>("ContractNumber")
                           equals customers.Field<String>("ContractNumber")
                           join SPListItem loans in _loanList
                           on contracts["ContractNumber"] equals loans["Contract_x0020_Number"] 
                           into JoinedContractLoans
                           from loans in JoinedContractLoans.DefaultIfEmpty()
                           select new { contracts, customers,loans };

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

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

发布评论

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

评论(1

趁年轻赶紧闹 2024-10-01 08:25:13

创建新的 DataRow实例并不可行,因为您无法直接访问 构造函数创建实例;您必须创建一个 DataTable ,其中包含您拥有的三个不同 DataTable 实例中的所有列,然后通过 DataTable 添加行。

虽然这当然是可行的,但需要做很多额外的工作。

您还可以创建一个匿名/具体类型,它具有来自单独的 DataTable 实例的所有字段,并填充该实例,如下所示:

// Rest of query the same.
select new { 
    ContractNumber = contracts.Field<string>("ContractNumber")
    // All other contract fields.

    // All customer fields.

    // All loan fields.
};

如果您正在使用匿名类型,那么这将很容易维护。然而,对于具体的类来说,它可能有点笨拙。

也就是说,您只剩下现在所拥有的,创建一个引用三个数据行的匿名类型。它可能是最容易维护的,假设使用它的代码采用相同的方法。

也就是说,如果您必须将其传递到方法之外,我建议使用强类型的 数据传输对象DataRow 映射到其中,然后传回三个数据传输对象的集合。

当然,如果您要走这条路,那么您应该查看 LINQ-to -Entities,因为这本质上是为了这个目的而设计的(它可能会简化很多代码)。

Creating a new DataRow instance isn't that feasible because you don't have direct access to the constructor to create an instance; you'd have to create a DataTable that contains all the columns from the three disparate DataTable instances you have and then add the rows through the DataTable.

While this is certainly doable, it's a lot of extra work.

You could also create an anonymous/concrete type that has all the fields from the separate DataTable instances and populate instances of that, like so:

// Rest of query the same.
select new { 
    ContractNumber = contracts.Field<string>("ContractNumber")
    // All other contract fields.

    // All customer fields.

    // All loan fields.
};

If you are working with anonymous types, then this would be easy to maintain. However, for a concrete class, it might be a little unwieldy.

That said, you're left with what you have now, creating an anonymous type which has references to the three data rows. It's probably the easiest to maintain, assuming the code that's working with this is in the same method.

That said, if you had to pass this outside a method, I'd recommend having a strongly-typed data transfer object which you map the DataRow to and then pass back the collection of the three data transfer objects.

Of course, if you're going that route, then you should look at LINQ-to-Entities, as this is essentially made for this purpose (it will probably simplify a lot of this code).

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