Навигация

Итоги года

Другие ссылки


Реклама

Счётчики


LINQ to SQL and Unions

22.08.2008 Пятница 17:50

For some reason I was getting the following error:

Specified cast is not valid

when trying to use LINQ to SQL in the following way:

var q1 = from p in Context.Posts
... blah ...
select new SearchResult
{
...
};

var q2 = q1.Union
(
from c in Context.PostComments
... blah ...
select new SearchResult
{
...
}
);

return q2.ToArray();
The error was happening at the last line. Turned out the following fixes it:

var q1 = from p in Context.Posts
... blah ...
select new SearchResult
{
...
};

var q2 = from c in Context.PostComments
... blah ...
select new SearchResult
{
...
};

return q2.ToArray().Union(q2.ToArray());