在不循环的情况下将

在不循环的情况下将

本文介绍了最佳实践:在不循环的情况下将 LINQ 查询结果转换为数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将 LINQ-Query 结果转换为新的 DataTable 的最佳实践是什么?
我能找到比 foreach 每个结果项更好的解决方案吗?

What is the best practice to convert LINQ-Query result to a new DataTable?
can I find a solution better than foreach every result item?

编辑匿名类型

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:我有例外:

本地序列不能在 LINQ to SQL 的查询运算符实现中使用,除了 Contains() 运算符.

当我尝试执行查询时并在此处找到了解决方案 IEnumerable.Except 不起作用,那怎么办
需要 linq 帮助

as I try to execute the queryand found the solution here IEnumerable.Except wont work, so what do I do?
and Need linq help

推荐答案

使用 Linq to Dataset.来自 MSDN:从查询创建数据表(LINQ to DataSet)

Use Linq to Dataset. From the 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>();

如果你有匿名类型:

来自编码器博客:使用 Linq 匿名类型和 CopyDataTable

它解释了如何使用 MSDN 的 如何:实现 CopyToDataTable Where the Generic Type T不是数据行

It explains how to use MSDN's How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow

这篇关于最佳实践:在不循环的情况下将 LINQ 查询结果转换为数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 20:24