Avatar of Charles Baldo
Charles Baldo
Flag for United States of America asked on

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
C#.NET ProgrammingMicrosoft SQL Server

Avatar of undefined
Last Comment
Fernando Soto

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Fernando Soto

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Charles Baldo

ASKER
Thank You
Fernando Soto

Not a problem charlesaldo, glad to help.
Your help has saved me hundreds of hours of internet surfing.
fblack61