connectseekers.com

Saturday, 17 May 2014

Random Row With LINQ To SQL Using C#.Net | Randomly Select Records Using Linq to SQL Using C#

5/17/2014 - By Pranav Singh 0

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.


So here is the article in c#.Net and VB.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


  Now run the application
Output 1
 Output 2

DOWNLOAD

Tags: , , ,
About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top