Click here to Skip to main content
16,020,249 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
var selpartNumber1 = from a in db.TEMPORARY_QUESTs
from b in db.QUESTIONs
from c in db.QUEST_SCATTERINGs
where a.QUESTIONNAIRE_NO == prm_quest_no &&
a.VERSION_NO == prm_version_no &&
a.MIKZOA_BHINA == prm_mikzoa_bhina &&
a.PATTERN_NUMBER == prm_pattern_number &&
a.GENERTED_DATE == ttaarich_last_use &&
a.GENERTED_TIME == (decimal)ttime_use &&
a.SERIAL_QESTION == b.SERIAL_QESTION &&
b.MIKZOA_BHINA == c.MIKZOA_BHINA &&
b.CHAPTER_CODE == c.CHAPTER_CODE &&
b.SUBJECT_CODE == c.SUBJECT_CODE &&
a.QUESTIONNAIRE_NO == c.QUESTIONNAIRE_NO &&
a.VERSION_NO == c.VERSION_NO &&
a.MIKZOA_BHINA == c.MIKZOA_BHINA &&
a.PATTERN_NUMBER == c.PATTERN_NUMBER &&
c.PART_NUMBER == 1
orderby c.PART_NUMBER ,
b.QUESTION_KIND ascending,
b.HISTOTY_DRAFTING ascending,
a.SEQ_4_QUESTION ascending
select new
{
a.SEQ_4_QUESTION,
a.SERIAL_QESTION,
b.HISTOTY_DRAFTING,
b.QUESTION_KIND
};

Random rand = new Random();
var shuffledList = selpartNumber1.OrderBy(x => rand.Next(1, selpartNumber1.Count())).ToList();

What I have tried:

advice ?????????????????????????????????????????????????????????????????
Posted
Updated 16-Nov-18 5:40am
Comments
Vincent Maverick Durano 14-Nov-18 16:05pm    
Which line causes the error? What are you trying to do?

1 solution

rand.Next(1, selpartNumber1.Count()) cannot be converted into a SQL query.

Since you're loading the entire set of results into memory, you need to do the sorting in-memory instead of in SQL.

You should also avoid counting the results of the query to generate the random number - especially as you're doing it in a loop! Order by Guid.NewGuid() instead.
C#
var shuffledList = selpartNumber1.AsEnumerable().OrderBy(x => Guid.NewGuid()).ToList();

NB: Using Guid.NewGuid() should also work if you're sorting in SQL; but there was a bug with queries that contained joins which caused duplicate data:
c# - Linq to Entities, random order - Stack Overflow[^]
 
Share this answer
 
Comments
Member 14054644 18-Nov-18 1:27am    
thanks you where more then helpfull

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900