Question

I have a view composed of a Union between two queries. For instance

SELECT num, title, discipline FROM Table1
UNION
SELECT number, heading, job FROM Table2

However, when I query the database using LINQ, only the results of the query at the top of the join (SELECT num, title, discipline FROM Table1) are outputted. The bottom query is not. Removing the top query therefore returns no value. The parameters of the model used to bind to the view matches the column name of the top query of the union query in the view.

public class mumodel
{
   pubic int num{get; set;}
   public string title{get; set;}
   public string discipline{get; set;}
}

I have tried to rename the columns of the second query as well but this does not work

SELECT num, title, discipline FROM Table1
UNION
SELECT number AS num, heading AS title, job AS discipline FROM Table2

My join queries execute well in SSMS with the correct count of records. However, the count of records in LINQ is just equal to that of the first query in the union. How else can i debug this please?

Was it helpful?

Solution

Please try the following code in your controller or in your repository if you use repository pattern...

IEnumerable<mumodel> result = dbContext.Database.SqlQuery<mumodel>("SELECT num, title, discipline FROM Table1
UNION
SELECT number AS num, heading AS title, job AS discipline FROM Table2");

Then, pass the result to the view..

return view(result);

I hope your problem might be okay.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top