I need to get data from to different databases and then combine these two data sets into a single IQueryable
collection.
Database #1 example of data:
-- SOLUTION #1
[ID] [int],
[Name] [nvachar](50),
[ExpirationDate] [datetime],
[Users] [int]
Databse #2 example of data:
-- SOLUTOIN #1
[ID] [int],
[Name] [nvarchar](50),
[Users] [int]
As you can see, the solution from database #2 doesn't have the attribute ExpirationDate
, which gives me some problems when doing the Union()
.
I have tried the following:
public static IQueryable GetAll()
{
var list = (from solution1 in db1.Solutions
select new
{
Id = solution1.ID,
Name = solution1.Name,
ExpirationDate = solution1.ExpirationDate,
Users = solution1.Users
})
.Union(from solution2 in db2.Solutions
select new{
Id = solution2.ID,
Name = solution2.Name,
ExpirationDate = (DateTime?) null,
Users = solution2.Users
});
}
But unfortunately, this doesn't work. When calling this, I get the following exception:
Object reference not set to an instance of an object.
I suppose that this is because I set the ExpirationDate = (DateTime?) null
.
I have also tried to get the data from SQL views, like so (this is an example of db2
as db1
is kind off self explanatory)
CREATE VIEW [dbo].[v_Solutions]
AS SELECT s.ID,
s.Name,
null AS [ExpirationDate],
s.Users
FROM Solution
An then altered the LINQ select new
statement to the following:
.Union(from solution2 in db2.Solutions
select new{
Id = solution2.ID,
Name = solution2.Name,
ExpirationDate = (DateTime?) solution2.ExpirationDate,
Users = solution2.Users
});
But doing this gives me a compile error:
Cannot convert type 'int?' to 'System.DateTime?'
Though the data in solution2.ExpirationDate
should just be null
.
I'm not quite sure how to complete this statement. Any ideas?