Link to home
Start Free TrialLog in
Avatar of Charles Baldo
Charles BaldoFlag for United States of America

asked on

LINQ statement to retrieve random record with GUID

Hello all,

I am a newbie to LINQ

Recently I posted a question to get a random record.  I was using  a two step approach to first get the list then use a second list with .skip()  and .take() and a random number using the max records in the returned list.  I wanted to do this in one LINQ statement.  One of the members here Fernando Soto,  submitted an idea to use GUID as part of the mix which is random and doing a orderby on it.  I though the idea was brilliant, however it did not work.  I like the approach and think it is close. Any help please?  And again thanks to Fernando for suggesting the idea.

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


Here was my first question that had my original (bloated) approach

https://www.experts-exchange.com/questions/28690437/Simplify-LINQ-statement-getting-random-record.html
Avatar of kaufmed
kaufmed
Flag of United States of America image

What do you mean by "it did not work?" What behavior did you observe?
Hi charlesbaldo;

I stated in my last post in the previous question that the Linq query was, "a Linq to Entity Framework query ", I have tested it in both EF 6 and EF 5 and it does work. It will NOT work if you are using Linq to SQL. Entity Framework correctly translates the Guid.NewGuid() method to the T-SQL NEWID() method where Linq to SQL just drops the complete line orderby.

I have not tested in earlier versions of EF below version 5.

What is your configuration charlesbaldo?
Avatar of Charles Baldo

ASKER

I am using SQL Server 2014, modeled with entity frameworks 6.0 and MVC 5
What I was seeing was the same record every time
Hi Chuck;

Did you place this line of the code, orderby Guid.NewGuid(), in the same place as shown in the question?
Yes I did,  I copied it exactly from my code to here. I know it has to be close.
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
Fernando,

Thank you

I am going to work this with LINQ pad.  I think its close and there is just something small I am overlooking.  If I still have issues I will re post a new issue with the reults

Thanks for all your help.  Again I though the idea was insightful and opened my eyes to the richness of LINQ
I did a test using a Linq query that had a join as well to make sure it was not the culprit causing the issue, it worked.

By the way when you executed the Linq query in LinqPad did the results come back in random order as it should have been?