gpt4 book ai didi

c# - 最佳实践 : Convert LINQ Query result to a DataTable without looping

转载 作者:IT王子 更新时间:2023-10-29 04:30:18 34 4
gpt4 key购买 nike

将 LINQ 查询结果转换为新的 DataTable 的最佳做法是什么?
我能否找到比 foreach 每个结果项更好的解决方案?

编辑 匿名类型

var rslt = from eisd in empsQuery
join eng in getAllEmployees()
on eisd.EMPLOYID.Trim() equals eng.EMPLOYID.Trim()
select new
{
eisd.CompanyID,
eisd.DIRECTID,
eisd.EMPLOYID,
eisd.INACTIVE,
eisd.LEVEL,
eng.EnglishName
};

编辑 2:我有异常(exception):

Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator.

当我尝试执行查询时并在这里找到解决方案 IEnumerable.Except wont work, so what do I do?
Need linq help

最佳答案

使用 Linq 到数据集。来自 MSDN:Creating a DataTable From a Query (LINQ to DataSet )

// Query the SalesOrderHeader table for orders placed 
// after August 8, 2001.
IEnumerable<DataRow> query =
from order in orders.AsEnumerable()
where order.Field<DateTime>("OrderDate") > new DateTime(2001, 8, 1)
select order;

// Create a table from the query.
DataTable boundTable = query.CopyToDataTable<DataRow>();

如果你有匿名类型:

来自编码器博客:Using Linq anonymous types and CopyDataTable

它解释了如何使用 MSDN 的 How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow

关于c# - 最佳实践 : Convert LINQ Query result to a DataTable without looping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4460654/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com