5

Good afternoon,

I have a listview filled using linqdatasource + entity framework iqueryable query.

The query uses a take (top on t-sql) like this:

context.Categories().OrderBy(c=>c.Name).Take(20);

So it brings me the 20 records I want ordered by name.

Now I want to show those 20 records on a random order. Whats the best approach acomplish this?

2 Answers 2

3

I believe the answer in this post is what you need:

Linq to Entities, random order

EDIT:

Get your top 20 records first. Then with the top 20 items you've already fetched, randomize them all in C#, not involving the database at all:

var yourRecords = context.Categories().OrderBy(c=>c.Name).Take(20); // I believe .Take() triggers the actual database call
yourRecords = yourRecords.OrderBy(a => Guid.NewGuid()); // then randomize the items now that they are in C# memory
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, thanks for your answer but is not what I need, because it would retrieve me 20 random records from whole data base table and I want to get 20 top records from data base and then show that 20 records on random order
that's a good and logical answer, it just leaves my linqdatasource out of the scenario. Do you think it can be done on DB?, like if you make this on TSQL select * from (select top 20 * from Categories order by Name) as Subquery order by NewGuid
Yep, that's definitely possible, too. ORDER BY NEWID() is the T-SQL for it: codekeep.net/snippets/1687fcea-5f5b-4c6d-b9d3-bd049667d2e1.aspx I assume you could wrap your original query, and then order by newid() in your outer query.
Hi (sorry for the delay), I've solved it (linq to entities) using an extension method, please see my answer and thank you
2

this turned out to be very simple using extension methods, ordering by name first, then calling Take (top on T-sql) and randomizing later

        context.Categories().OrderByName().Take(20).OrderByRandom();


        public static IQueryable<Category> OrderByName(this IQueryable<Category> query)
        {
                return from c in query
                        orderby c.Name
                        select c;
        }



        public static IQueryable<T> OrderByRandom<T>(this IQueryable<T> query)
        {
                return (from q in query
                        orderby Guid.NewGuid()
                        select q);
        }

2 Comments

this may work using pure lambda expression without extension methods but I haven't tested it
Cool, glad you got it working. Are the try/catches needed? If all they do is throw the exception, isn't that the default behavior without them?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.