Get Random Records from Generic List (List) using LINQ in C# .Net

motisariya
 
on Apr 11, 2014 12:53 AM
4627 Views

This My code

var id = (from a in linq_obj.product_masters
        where a.fearured_produ == "True"           
        select new
        {
            code = a.intGlCode,
            img = "./upload/" + a.main_images,
            rs = "र " + a.discount_price,
            title = a.var_name,
            disc = a.description
        }).ToList();
DataList_feture_product.DataSource = id;
DataList_feture_product.DataBind();

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Mudassar
 
on Apr 11, 2014 12:55 AM

Check this example

Class

public class Person
{
    public string Name { get; set; }
}

Added sone sample data

List<Person> persons = new List<Person>();
persons.Add(new Person { Name = "Mudassar Khan" });
persons.Add(new Person { Name = "Albert Schimdt" });
persons.Add(new Person { Name = "Jane Thomas" });
persons.Add(new Person { Name = "Raj Malhotra" });
persons.Add(new Person { Name = "Abbas Shaikh" });

Select Random records

int rnd = new Random().Next(persons.Count);
List<Person> random_persons = persons.OrderBy(g => Guid.NewGuid()).ToList();