简体   繁体   中英

Using join and pivot in entity framework with C# or write a stored procedure

I want to create a custom joined table and want to show rows to the columns. I want to get Invitation informations that are default but joined with the question answers that people give to any invitation.

I need question and answer rows to the columns. (I think we should use pivot but i am not sure.)

I couldnt able to find a way to do this in ef c# using lambda or linq. Or I'm using sql server 2012, so if you give me the sql statement for this purpose, i will be really appreciated.

Here are my tables.

People

id | name       | surname  
---+------------+------------
 1 |    carl    | sagan  
 2 |    john    | wick   
 3 |    alex    | rony   
 4 |    mary    | olsen   

Invitation

 id | name       | peopleid  | hasquestion | isdefault  
---+-------------+-----------+-------------+-----------
 1 |    inv1     | 1         |    0        |    1
 2 |    inv2     | 1         |    1        |    0
 3 |    inv3     | 2         |    0        |    0
 4 |    inv4     | 2         |    1        |    1
 5 |    inv5     | 2         |    1        |    0
 6 |    inv6     | 3         |    1        |    1
 7 |    inv7     | 4         |    0        |    1

Question

 id | text         
---+------------
 1 |    question 1    
 2 |    question 2       
 3 |    question 3        
 4 |    question 4
 5 |    question 5  

QuestionAnswers

id | invitationid | questionid  | answer
---+--------------+-------------+--------
 1 |    2         | 1           |    abc
 2 |    2         | 2           |    def
 3 |    2         | 3           |    ghi
 4 |    4         | 3           |    jkl
 5 |    4         | 4           |    mno
 6 |    5         | 1           |    prs    
 7 |    6         | 5           |    tuy    

My result table should be:

id |  peoplename     | default   | question 1  | question 2 | question 3 | question 4 | question 5  
---+-----------------+-----------+-------------+------------+------------+------------+-----------
 1 |    carl sagan   | inv1      |    abc      |    def     |   ghi      |            |   
 2 |    john wick    | inv4      |    prs      |            |   jkl      |    mno     |   
 3 |    alex rony    | inv6      |             |            |            |            |    tuy
 3 |    mary olsen   | inv7      |             |            |            |            |    

You can run this script to create your SQL procedure first.

CREATE PROCEDURE sp_get_question_answers
AS
SELECT 
[peoplename],
[invitation],
[question 1],
[question 2],
[question 3],
[question 4],
[question 5]
FROM 
(SELECT 
p.[name] + ' ' + p.surname AS [peoplename],
i.[name] AS invitation,
q.[text] question,
qa.answer
 FROM dbo.people p  
 INNER JOIN dbo.invitations i ON p.id = i.people_id
 INNER JOIN dbo.question_answers qa ON i.id = qa.invitation_id
 LEFT JOIN dbo.questions q ON qa.question_id = q.id)
 AS SourceTable
 PIVOT 
 (
 MAX(answer)
 FOR question  IN 
([question 1],
[question 2],
[question 3],
[question 4],
[question 5]) 
)AS PivotTable;
GO

Then you can execute procedure via entity framework. IEnumerable<QuestionAnswer> answers = dbContext.FromSql<QuestionAnswer>("Exec sp_get_question_answers");

Assuming QuestionAnswer is a class that has properties matching those returned from the procedure.

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