简体   繁体   English

通过LINQ查询循环并将结果附加到DataTable

[英]Looping Through LINQ Queries and Appending Results to DataTable

Im trying to use a loop to cycle through an array, and in each iteration perform a LINQ query using the arrays value in the queries where clause. 我试图使用循环遍历数组,并在每次迭代中使用查询where子句中的数组值执行LINQ查询。

What I then want to do is append these results successively together and then return the results as a DataTable. 我当时想要做的是将这些结果连续地附加在一起,然后将结果作为DataTable返回。

How can I append the results together? 如何将结果附加在一起? I have tried the DataTable .Merge() method, but receive an error ("Cannot implicitly convert type void to DataTable"), and cannot work out how to append the queries results due to generic type scoping problems. 我已经尝试了DataTable .Merge()方法,但是收到错误(“无法将类型void隐式转换为DataTable”),并且由于泛型类型范围问题而无法解决如何追加查询结果的问题。

EDIT: The code... (or at least one of the versions I have tried so far) 编辑:代码...(或至少我迄今为止尝试过的一个版本)

    DataTable results = new DataTable();

    foreach (string toFind in searchString)
    {

        DataTable toMerge = new DataTable();

        var searchResults = from a in dt.AsEnumerable()
                            where a.Field<string>("A").ToUpper().Contains(toFind.ToUpper())
                            select new
                            {
                                A= a.Field<Int32>("A"),
                                B= a.Field<string>("B"),
                                C= a.Field<string>("C"),
                            };

        toMerge = ConvertDataTable.FromLINQ(searchResults.AsQueryable()); // Method I have to convert the LINQ query to a DataTable

        results = results.Merge(toMerge);

    }

Any ideas? 有任何想法吗?

Thanks in advance 提前致谢

Chris 克里斯

You just need do results.Merge(toMerge); 你只需要做results.Merge(toMerge); you shouldn't do results = results.Merge(toMerge); 你不应该做results = results.Merge(toMerge); . Because Merg function acts on caller DataTable, and doesn't return anything. 因为Merg函数作用于调用者DataTable,并且不返回任何内容。

I believe using Merge() and ConvertDataTable.FromLINQ() each loop cycle is pretty expensive. 我相信使用Merge()ConvertDataTable.FromLINQ()每个循环周期都相当昂贵。 Why just not do a results.Rows.Add() in the same time as querying the search results? 为什么不在查询搜索结果的同时执行results.Rows.Add()

foreach (string toFind in searchString)     
{
   var searchResults = dt.AsEnumerable()  
                         .Where(a => a.Field<string>("A")
                                      .ToUpper()
                                      .Contains(toFind.ToUpper())
                        .Select(a => new object[] {
                                           a.Field<Int32("A"),
                                           a.Field<string>("B"),
                                           a.Field<string>("C")
                         })
                        .ForEach(re => results.Rows.Add(re));
}


// ForEach() extension method would help you adding rows to a data table
// in the same time as querying the results
public static void ForEach<T>(
    this IEnumerable<T> source,
    Action<T> action)
{
    foreach (T element in source) 
        action(element);
}

If you really have a lot of items it would be interesting measuring the actual performance of the loop execution, and please share results if you could: 如果你真的有很多项目,那么测量循环执行的实际性能会很有趣,如果可以的话, 分享结果:

var stopwatch = System.Diagnostics.Stopwatch.StartNew();
foreach () { ... }
stopwatch.Stop;
double totalMilliseconds = stopwatch.TotalMilliseconds;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM