I am trying to get the subset of items in dataA that are in dataB, and have different values of property c. The properties a and b can be used as an index, so I have tried to filter out only the useful pairs then check to see if they have a different c value.
This is the linq expression I came up with, and it does work, but It seems like there has to be a better/faster way of finding this subset.
var itemsInBoth = from item in dataA from item2 in dataB where item.a == item2.a && item.b == item2.b select new { first= item, second = item2 }; var haveDifferentC = from item in itemsInBoth where item.first.c != item.second.c select item.first;
Faster? What you have there is O(n^2). Each item in the first list will fully iterate the items in the second list. You need to remove the redundant iteration in that join. One way to do that is to use another structure to do O(1) lookups for matchs.
Here’s some untested (unchecked) code:
Here’s a simplified version if a,b pairs are unique in each list.