1

I have tried following code i need to random row with out duplication based on the NAME field.

List list = db.Logos.Where(a => a.LogoName != "empty.png").OrderBy(r => Guid.NewGuid()) .GroupBy(a => a.LogoName) .Select(g => g.FirstOrDefault()).Take(8).ToList();

but i did not get randomly but i have tried another code which is shuffling correctly but duplicate code could not apply, code below

List list = (from c in db.Logos where c.LogoName != "empty.png" select c).OrderBy(r => Guid.NewGuid()).Take(3).ToList();

please help how to select both duplication and random rows.

2 Answers 2

2

Can you please try the below one?

var result = (from logo in db.Logos
             where (logo.LogoName != "empty.png")
             orderby logo.LogoName
             select logo).Distinct();
Sign up to request clarification or add additional context in comments.

3 Comments

This code is fine but i need randomly select rows with that code
Do you want random records, for each query execution?
Are you storing the previous query result somewhere?
0

You are doing the ordering before the grouping. That looses the ordering completely.

db.Logos
.Where(a => a.LogoName != "empty.png")
.GroupBy(a => a.LogoName)
.Select(g => g.FirstOrDefault())
.OrderBy(r => Guid.NewGuid())
.Take(8)
.ToList();

Performance of this query will not be so great, but that does not seem to be your primary concern. It will be fast on small data sets.

Comments

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.