In some cases we have seen that if we are using linq for
fetching the record and fetching it randomly. So in this article I will show
you how to display Randomly Select Records Using Linq to SQL using c#. This
article we can use in asp.net, mvc application.
Some of my previous articles are as follows : Merge
two datatables in C#.Net and VB.Net | How to merge multiple datatables using
C#.Net , Random
Number Generator C# , How
to show error message in c# windows application | How to show Error &
Warning Message Box in .NET , How
to create a Xml Document Programmatically Using C#.Net .
C#.Net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data;
namespace ProjectDemo
{
class Program
{
//How
toMerg both the datatable Programmatically Using C#.Net
static
void Main(string[]
args)
{
List<Student> objstudent = new List<Student>();
objstudent = StudentData();//Here you can pass your own data base collection
var
studentRecord = (from data in objstudent
select data).OrderBy(x => Guid.NewGuid()).Take(50);
foreach
(Student item in
studentRecord)
{
Console.WriteLine("Name :" + item.Name + " ,Roll No : " + item.RollNo + " ,Marks :" + item.Marks);
}
Console.ReadLine();
}
//Data to
our list...
public
static List<Student> StudentData()
{
List<Student> _objStudent = new List<Student>();
_objStudent.Add(new Student {
Name = "Ram", RollNo = 1, Marks
= 85.5 });
_objStudent.Add(new Student {
Name = "vinay", RollNo = 2,
Marks = 95.5 });
_objStudent.Add(new Student {
Name = "pranac", RollNo = 3,
Marks = 75.5 });
return
_objStudent;
}
}
public class Student
{
public
string Name { get;
set; }
public
int RollNo { get;
set; }
public
double Marks { get;
set; }
}
}
|
VB.Net
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Xml
Imports System.Data
Namespace ProjectDemo
Class Program
'How toMerg
both the datatable Programmatically Using C#.Net
Private
Shared Sub
Main(ByVal args As
String())
Dim
objstudent As New
List(Of Student)()
objstudent = StudentData()
'Here
you can pass your own data base collection
Dim
studentRecord = (From data In objstudentdata).OrderBy(Function(x)
Guid.NewGuid()).Take(50)
For
Each item As
Student In studentRecord
Console.WriteLine("Name :" & item.Name & " ,Roll No : " & item.RollNo &
" ,Marks :" & item.Marks)
Next
Console.ReadLine()
End Sub
'Data to
our list...
Public
Shared Function
StudentData() As List(Of Student)
Dim
_objStudent As New
List(Of Student)()
_objStudent.Add(New Student() With
{ _
Key .Name = "Ram", _
Key .RollNo = 1, _
Key .Marks = 85.5 _
})
_objStudent.Add(New Student() With
{ _
Key .Name = "vinay", _
Key .RollNo = 2, _
Key .Marks = 95.5 _
})
_objStudent.Add(New Student() With
{ _
Key .Name = "pranac", _
Key
.RollNo = 3, _
Key .Marks = 75.5 _
})
Return
_objStudent
End Function
End Class
Public Class Student
Public
Property Name() As
String
Get
Return
m_Name
End
Get
Set(ByVal value As String)
m_Name = Value
End
Set
End Property
Private
m_Name As String
Public
Property RollNo() As
Integer
Get
Return
m_RollNo
End
Get
Set(ByVal value As Integer)
m_RollNo = Value
End
Set
End Property
Private
m_RollNo As Integer
Public
Property Marks() As
Double
Get
Return
m_Marks
End
Get
Set(ByVal value As Double)
m_Marks = Value
End
Set
End Property
Private
m_Marks As Double
End Class
End Namespace
|
Output 1
Output 2
DOWNLOAD
0 comments:
Please let me know your view