简体   繁体   中英

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.

What I then want to do is append these results successively together and then return the results as a 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.

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); you shouldn't do results = results.Merge(toMerge); . Because Merg function acts on caller DataTable, and doesn't return anything.

I believe using Merge() and ConvertDataTable.FromLINQ() each loop cycle is pretty expensive. Why just not do a results.Rows.Add() in the same time as querying the search results?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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