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.
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[
^]