Link to home
Start Free TrialLog in
Avatar of Charles Baldo
Charles BaldoFlag 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
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Charles Baldo

ASKER

Thank You
Not a problem charlesaldo, glad to help.