question

Zackelberry-2221 avatar image
0 Votes"
Zackelberry-2221 asked Viorel-1 answered

How To execute this scenario on EF Core

Hello,

This is an exam program where Table B is all the question to the exam and Table A is the answers of the user.
each question has two choices AnswerID that is why the QuestionId appears twice in one Group in TableA. Once the user answered all the question. which is the TableA.ID 1 to TableA.ID 6. he finishes the exam. now the user is allowed to retake the exam. This TableA.ID 7 to TableA.ID 12 is the retake.
What i want to achieve is to get how many times does the user took the exam. that's why in this example the answer is 2.

Please see Image
136600-tables.png



I have this query base on the tables on the image

 var tableBquestionIDs = await _dbContext.TableB.Select(q => 
                               q.QuestionID).toListAsync();
    
 var tableAQuestionIDs = await _dbContext.TableA.Where(r => 
                            tableBquestionIDs.Contains(r.QuestionID))
                            .Select(r => r.QuestionID)
                           .ToListAsync();




can someone help me how am I able to accomplish this on Ef Core
as I was stock here for days
TIA

sql-server-generaldotnet-csharpdotnet-entity-framework-core
tables.png (24.0 KiB)
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,

I need to get how many times the value of tableBquestionIDs appears on tableAQuestionIDs as a group... In this example the result is 2

(1) Probably I did not understood you well since according to the image I see that all the values 299, 295, 298 appear in the table A. How did you got the expected number 2 and not 3?

(2) If you want to get the amount of entitles then you need a count which you do not have. Missing the main part of the code?


0 Votes 0 ·

Ok let me clarify. the value from Table B must be counted as a group.
if you notice from TableA.ID 1 to TableA.ID 6 if you take out the duplicate value you get 299, 295, 298 and that is one group
and must be counted as 1

then from TableA.ID 7 go TableA.ID 12. if again remove the duplicate that is another group of 299, 295, 298 so that makes it 2

0 Votes 0 ·
pituach avatar image pituach Zackelberry-2221 ·

Hi,

What make the groups to be rows 1-6 and 7-12 instead of 1-5 and 6-12 or maybe 1-2,3-5,6-8,9-12...

There is no column in the table which give us the information of group. What do you group by?

0 Votes 0 ·
Show more comments
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered Zackelberry-2221 commented

The first step is to create a GroupBy in a lambda or linq query with a where condition on a list containing QuestionId in Table B. Once you have the results either use them or apply another where predicate to the first result set. Test this in unit test methods.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This is the real scenario. this is an exam program where Table B is all the question to the exam and Table A is the answers of the user.
each question has two choices that is why the QuestionId appears twice in one scenario in TableA. Once the user answer all the question. which is the TableA.ID 1 to TableA.ID 6. he finishes the exam. now the user is allowed to retake the exam. This TableA.ID 7 go TableA.ID 12
is the retake. what i want to achieve is how many times does the user took the exam. that's why in this example the answer is 2.

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered

Maybe the next answer correspond to your question:

int how_many_times = _dbContext.TableA.Count( ) / (_dbContext.TableB.Count( ) * 2);


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.