Thursday, July 18, 2013

Sunday, July 14, 2013

How to get Top N result From Linq?

datatable dt1 = (from result in dt1.AsEnumerable().OrderBy(result => Guid.NewGuid()) select result).Take(10).ToList().CopyToDataTable();

How to get top N Result from dataview or datatable in C#?

public static DataView SelectTopFrom(DataView dv, int rowCount)

{

DataTable dt = dv.ToTable();

DataTable dtn = dt.Clone();

for (int i = 0; i < rowCount; i++)

{

dtn.ImportRow(dt.Rows[i]);

}

DataView dvn = new DataView(dtn);

return dvn;

}