Simplify LINQ statement getting random record

Hello I have this LINQ statement.  I want to know if anyone sees a way to simplify it so I do not have to get a list then take from that list a random record

       Random rand = new Random();            
      var AdList =
                (from a in Advertisements
                 join c in Companies on a.AdvertiementOwnerID equals c.CompanyID
               where a.AdvertisementType=="Banner"
                 select new {
                    a.AdvertisementID,
                        a.AdvertisementType,
                    a.AdvertiementActive,
                    a.AdvertisementText,
                    a.AdvertisementImageLocation,
                    a.AdvertisementAlt,
                    c.CompanyName,
                    c.Website }
                ).ToList();      <-- would like to do all here                  
                        
         int toSkip = rand.Next(0, AdList.Count);  problem here do not know how to get count in above list and use it as random Max val
            var AdList2 = AdList.Skip(toSkip).Take(1);

          AdList2.Dump();      // LINQPad dumps
Charles BaldoSoftware DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi charlesbaldo;

Here is a Linq to Entity Framework query which will return random records from the table. It can be configured to return one, two or more or all in random order. And the ordering is done on the server.

var AdList =
    (from a in Advertisements
     join c in Companies on a.AdvertiementOwnerID equals c.CompanyID
     where a.AdvertisementType=="Banner"
     orderby Guid.NewGuid()
     select new {
        a.AdvertisementID,
        a.AdvertisementType,
        a.AdvertiementActive,
        a.AdvertisementText, 
        a.AdvertisementImageLocation, 
        a.AdvertisementAlt, 
        c.CompanyName,
        c.Website 
    }).ToList();          // This will return all records in random order
    
//  }).FirstOrDefault();  // This will return one records randomly each time  
//  }).Take(X);           // This will return X random record

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Charles BaldoSoftware DeveloperAuthor Commented:
Thank You
0
Fernando SotoRetiredCommented:
Not a problem charlesaldo, glad to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.