DataTable Query
I am new to LINQ. I am trying to find rows that do not exist in the second data table.
report_list and both test types are: DataTable. Both of these data types are populated using the OleDbCommand, OleDbDataAdapter. I get the error "The specified cast is not valid." in foreach ... loop. I would appreciate your help.
var result = from a in report_list.AsEnumerable()
where !(from b in benchmark.AsEnumerable()
select b.Field<int>("bench_id")
)
.Contains(a.Field<int>("BenchmarkID"))
select a;
foreach (var c in result)
{
Console.WriteLine(c.Field<string>("Name"));
}
I don't know if I understood your question. Are you trying to get items that exist in the first table but not the second?
var first = new string[] { "b", "c" };
var second = new string[] { "a", "c" };
//find the itens that exist in "first" but not in "second"
var q = from f in first
where !second.Contains(f)
select f;
foreach (var s in q) {
Console.WriteLine(s);
}
//Prints:
//b
I suggest doing an inner query first if it doesn't depend on the outer record.
source to share
Perhaps use the .Except () extension to get the difference between two sets?
(from b in benchmark.AsEnumerable()
select new { id = b.Field<int>("bench_id")}).Except(
from a in report_list.AsEnumerable()
select new {id = a.Field<int>("BenchmarkID")})
Not really sure about the exact syntax, but this should work with the IDs in the template and then remove all the equivalent IDs in the report list, leaving only IDs that don't match. (I hope this is your order after ...)
Note. This also assumes that the above problem mentioned by tvanfosson is not a problem either.
source to share