C# EF Random Order causes related list disappear












0















I got a Problem with my API. I want to get a random amout of questions back.



The Model of my question has a list of answers and hints:



public class Question
{
public int Id { get; set; }
public string Text { get; set; }
public string Explanation { get; set; }
public Category Category { get; set; }
public ICollection<Answer> Answers { get; set; }
public ICollection<Hint> Hints { get; set; }
}


normally if i call my get method i get a json with all the lists back



return _ctx.Questions.Include(x => x.Answers).Include(x => x.Hints).ToList();

{
"id": 1,
"text": "Test?",
"explanation": "Test",
"category": null,
"answers": [
{
"id": 1,
"text": "Test",
"isCorrect": true
},
{
"id": 2,
"text": "Test1",
"isCorrect": false
},
{
"id": 3,
"text": "Test2",
"isCorrect": false
},
{
"id": 4,
"text": "Test3",
"isCorrect": false
}
],
"hints": [
{
"id": 1,
"text": "..."
},
{
"id": 2,
"text": "..."
}
]
}


But if I want to get random picks with a orderby i only got empty lists



return _ctx.Questions.Include(x => x.Answers).Include(x => x.Hints).OrderBy(o => Guid.NewGuid()).Take(amount).ToList();

{
"id": 1,
"text": "test",
"explanation": "..-",
"category": null,
"answers": ,
"hints":
}


Does someone have a Idea to fix this?










share|improve this question

























  • According to this answer, you should be using a Fisher-Yates Shuffle algorithm.

    – ikerbera
    Nov 23 '18 at 11:06
















0















I got a Problem with my API. I want to get a random amout of questions back.



The Model of my question has a list of answers and hints:



public class Question
{
public int Id { get; set; }
public string Text { get; set; }
public string Explanation { get; set; }
public Category Category { get; set; }
public ICollection<Answer> Answers { get; set; }
public ICollection<Hint> Hints { get; set; }
}


normally if i call my get method i get a json with all the lists back



return _ctx.Questions.Include(x => x.Answers).Include(x => x.Hints).ToList();

{
"id": 1,
"text": "Test?",
"explanation": "Test",
"category": null,
"answers": [
{
"id": 1,
"text": "Test",
"isCorrect": true
},
{
"id": 2,
"text": "Test1",
"isCorrect": false
},
{
"id": 3,
"text": "Test2",
"isCorrect": false
},
{
"id": 4,
"text": "Test3",
"isCorrect": false
}
],
"hints": [
{
"id": 1,
"text": "..."
},
{
"id": 2,
"text": "..."
}
]
}


But if I want to get random picks with a orderby i only got empty lists



return _ctx.Questions.Include(x => x.Answers).Include(x => x.Hints).OrderBy(o => Guid.NewGuid()).Take(amount).ToList();

{
"id": 1,
"text": "test",
"explanation": "..-",
"category": null,
"answers": ,
"hints":
}


Does someone have a Idea to fix this?










share|improve this question

























  • According to this answer, you should be using a Fisher-Yates Shuffle algorithm.

    – ikerbera
    Nov 23 '18 at 11:06














0












0








0








I got a Problem with my API. I want to get a random amout of questions back.



The Model of my question has a list of answers and hints:



public class Question
{
public int Id { get; set; }
public string Text { get; set; }
public string Explanation { get; set; }
public Category Category { get; set; }
public ICollection<Answer> Answers { get; set; }
public ICollection<Hint> Hints { get; set; }
}


normally if i call my get method i get a json with all the lists back



return _ctx.Questions.Include(x => x.Answers).Include(x => x.Hints).ToList();

{
"id": 1,
"text": "Test?",
"explanation": "Test",
"category": null,
"answers": [
{
"id": 1,
"text": "Test",
"isCorrect": true
},
{
"id": 2,
"text": "Test1",
"isCorrect": false
},
{
"id": 3,
"text": "Test2",
"isCorrect": false
},
{
"id": 4,
"text": "Test3",
"isCorrect": false
}
],
"hints": [
{
"id": 1,
"text": "..."
},
{
"id": 2,
"text": "..."
}
]
}


But if I want to get random picks with a orderby i only got empty lists



return _ctx.Questions.Include(x => x.Answers).Include(x => x.Hints).OrderBy(o => Guid.NewGuid()).Take(amount).ToList();

{
"id": 1,
"text": "test",
"explanation": "..-",
"category": null,
"answers": ,
"hints":
}


Does someone have a Idea to fix this?










share|improve this question
















I got a Problem with my API. I want to get a random amout of questions back.



The Model of my question has a list of answers and hints:



public class Question
{
public int Id { get; set; }
public string Text { get; set; }
public string Explanation { get; set; }
public Category Category { get; set; }
public ICollection<Answer> Answers { get; set; }
public ICollection<Hint> Hints { get; set; }
}


normally if i call my get method i get a json with all the lists back



return _ctx.Questions.Include(x => x.Answers).Include(x => x.Hints).ToList();

{
"id": 1,
"text": "Test?",
"explanation": "Test",
"category": null,
"answers": [
{
"id": 1,
"text": "Test",
"isCorrect": true
},
{
"id": 2,
"text": "Test1",
"isCorrect": false
},
{
"id": 3,
"text": "Test2",
"isCorrect": false
},
{
"id": 4,
"text": "Test3",
"isCorrect": false
}
],
"hints": [
{
"id": 1,
"text": "..."
},
{
"id": 2,
"text": "..."
}
]
}


But if I want to get random picks with a orderby i only got empty lists



return _ctx.Questions.Include(x => x.Answers).Include(x => x.Hints).OrderBy(o => Guid.NewGuid()).Take(amount).ToList();

{
"id": 1,
"text": "test",
"explanation": "..-",
"category": null,
"answers": ,
"hints":
}


Does someone have a Idea to fix this?







c# entity-framework repository






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 11:20







user3559349

















asked Nov 23 '18 at 10:54









MiroBehnMiroBehn

33




33













  • According to this answer, you should be using a Fisher-Yates Shuffle algorithm.

    – ikerbera
    Nov 23 '18 at 11:06



















  • According to this answer, you should be using a Fisher-Yates Shuffle algorithm.

    – ikerbera
    Nov 23 '18 at 11:06

















According to this answer, you should be using a Fisher-Yates Shuffle algorithm.

– ikerbera
Nov 23 '18 at 11:06





According to this answer, you should be using a Fisher-Yates Shuffle algorithm.

– ikerbera
Nov 23 '18 at 11:06












1 Answer
1






active

oldest

votes


















0














After sql need to be a list. I got a similar problem a long time ago.
Hope it s help, was with an older version of Ef.



So you have to add a ToList before the OrderBy.



return _ctx.Questions.Include(x => x.Answers).Include(x => x.Hints).ToList().OrderBy(o => Guid.NewGuid()).Take(amount).ToList();






share|improve this answer
























  • Worked! Thank you!

    – MiroBehn
    Nov 23 '18 at 11:10











  • Works, but loads the whole table (along with the related data) in memory.

    – Ivan Stoev
    Nov 23 '18 at 11:55











  • Take out the .ToList() in the middle.

    – Neil
    Nov 23 '18 at 11:56











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445333%2fc-sharp-ef-random-order-causes-related-list-disappear%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














After sql need to be a list. I got a similar problem a long time ago.
Hope it s help, was with an older version of Ef.



So you have to add a ToList before the OrderBy.



return _ctx.Questions.Include(x => x.Answers).Include(x => x.Hints).ToList().OrderBy(o => Guid.NewGuid()).Take(amount).ToList();






share|improve this answer
























  • Worked! Thank you!

    – MiroBehn
    Nov 23 '18 at 11:10











  • Works, but loads the whole table (along with the related data) in memory.

    – Ivan Stoev
    Nov 23 '18 at 11:55











  • Take out the .ToList() in the middle.

    – Neil
    Nov 23 '18 at 11:56
















0














After sql need to be a list. I got a similar problem a long time ago.
Hope it s help, was with an older version of Ef.



So you have to add a ToList before the OrderBy.



return _ctx.Questions.Include(x => x.Answers).Include(x => x.Hints).ToList().OrderBy(o => Guid.NewGuid()).Take(amount).ToList();






share|improve this answer
























  • Worked! Thank you!

    – MiroBehn
    Nov 23 '18 at 11:10











  • Works, but loads the whole table (along with the related data) in memory.

    – Ivan Stoev
    Nov 23 '18 at 11:55











  • Take out the .ToList() in the middle.

    – Neil
    Nov 23 '18 at 11:56














0












0








0







After sql need to be a list. I got a similar problem a long time ago.
Hope it s help, was with an older version of Ef.



So you have to add a ToList before the OrderBy.



return _ctx.Questions.Include(x => x.Answers).Include(x => x.Hints).ToList().OrderBy(o => Guid.NewGuid()).Take(amount).ToList();






share|improve this answer













After sql need to be a list. I got a similar problem a long time ago.
Hope it s help, was with an older version of Ef.



So you have to add a ToList before the OrderBy.



return _ctx.Questions.Include(x => x.Answers).Include(x => x.Hints).ToList().OrderBy(o => Guid.NewGuid()).Take(amount).ToList();







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 11:06









LeBigCatLeBigCat

2087




2087













  • Worked! Thank you!

    – MiroBehn
    Nov 23 '18 at 11:10











  • Works, but loads the whole table (along with the related data) in memory.

    – Ivan Stoev
    Nov 23 '18 at 11:55











  • Take out the .ToList() in the middle.

    – Neil
    Nov 23 '18 at 11:56



















  • Worked! Thank you!

    – MiroBehn
    Nov 23 '18 at 11:10











  • Works, but loads the whole table (along with the related data) in memory.

    – Ivan Stoev
    Nov 23 '18 at 11:55











  • Take out the .ToList() in the middle.

    – Neil
    Nov 23 '18 at 11:56

















Worked! Thank you!

– MiroBehn
Nov 23 '18 at 11:10





Worked! Thank you!

– MiroBehn
Nov 23 '18 at 11:10













Works, but loads the whole table (along with the related data) in memory.

– Ivan Stoev
Nov 23 '18 at 11:55





Works, but loads the whole table (along with the related data) in memory.

– Ivan Stoev
Nov 23 '18 at 11:55













Take out the .ToList() in the middle.

– Neil
Nov 23 '18 at 11:56





Take out the .ToList() in the middle.

– Neil
Nov 23 '18 at 11:56


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445333%2fc-sharp-ef-random-order-causes-related-list-disappear%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...