简体   繁体   中英

Linq to SQL union data from two tables into single IEnumerable/IQueryable

I have two log tables in seperate SQL Server databases (on different servers). The two tables have exactly the same schema. In my .NET application I can create two Linq to SQL class collections (DBML files) and then do my own custom Linq queries to union the two tables and return an anonymous type. But I have several queries (all slightly different) on the same tables and doing the unions every time is repetitive.

Is it possible to union the two tables into a single concrete/named type IEnumerable? Basically what I'd like to do is something like:

var logs = Logs.Where(l => l.TimeStart >= DateTime.Today)

And have Logs actually return data from two tables and have sensible queries sent to the two database servers no matter what I put in the .Where() .

Is this possible?

I believe that out of the box you cannot get one IQueryable covering multiple datacontexts. You mention you have

seperate SQL Server databases (on different servers).

A solution might be to create a linked view. Example: Create View using Linked Server db in SQL Server

Once you have the linked view, the query will be fairly simple and straigtforward.

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