Using Entity Framework, I'm trying to work around the fact that Linq can't handle joins between tables from different databases. The databases:
DB1.dbo.Table1: Id, XmlText
DB2.dbo.Table2: Id, TimeStamp, ColA, ColB...
I want to find the rows in Table2 where a property in XmlText is set to true, that is, XmlText contains 'xmlProp=true'
In SQL, I would simply write
SELECT *
FROM [DB2].[dbo].[Table2]
INNER JOIN [DB1].[dbo].[Table1] ON [Table1].Id = [Table2].Id
WHERE XmlText like '%xmlProp=true%'
Since I can't use join with Linq (different databases, remember?), I'm doing:
var query = from t1 in db1.Table1
where db1.Text.Contains('<xmlProp>false</xmlProp>)
select t1.Id;
var set = new HashSet<Guid>();
foreach (var elem in query)
set.Add(elem);
var query2 = from t2 in db2.Table2
where !set.Contains(t2.Id)
select t2;
I could of course search for 'true' instead, but fewer rows has false so the set will be smaller, thus increasing performance.
Now: is there a better way to do this? The SQL queries that are generated are massive (long list of ID's to avoid...), and it feels stupid to store the ID's in the memory just to pass them back to the SQL server again. Anyone?
Thanks, M