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 doresults = results.Merge(toMerge);. BecauseMergfunction acts on caller DataTable, and doesn’t return anything.