Light
Light

Reputation: 1097

How to select records from two tables randomly using linq

I want to query from database and select 50 records randomly from one table, and one record from another table which will be always available in the random list using LINQ.

I searched in google but couldn't get any help so I posted here.

So far I got random rows but 1 specific record is not shown...

public List<TweetFollowers> GetRandomTweets(int Skip, int Take)
{
    List<TweetFollowers> tweetList;
    using (var context = new DBContext())
    {
        var tweets = context.KKRTweetFollower.OrderBy(x => Guid.NewGuid()).Take(Take).Skip(Skip);
        tweetList = tweets.ToList();
    }
    return tweetList;
}

Upvotes: 0

Views: 165

Answers (1)

user3475921
user3475921

Reputation:

here's my solution for the problem:

public static Quotes GetRandomQuotes()
{

HsInternetDBDataContext db = new HsInternetDBDataContext();

var query = from c in db.Quotes select c;
int count = query.Count();
if (query.Count() > 0)
{
    Random r = new Random();
    return new Quotes(query.ToList()[r.Next(0, count)]);
}
else
{
    return new Quotes();
}
}

Upvotes: 1

Related Questions