Friday, 25 May 2012

C# Programming : Different LINQ operators in LINQ To Objects

LINQ also includes many operators you can execute on enumerable objects.
Most of these operators are available to use and are similar to operators that you find in SQL, such as Count, Min, Max, Average, and Sum.
Here are sample example of using this operators.

C# Example :
    public class Books
        public string Title { get; set; }
        public string ISBN { get; set; }
        public DateTime ReleaseDate { get; set; }
        public int Pages { get; set; }

    protected void Page_Load(object sender, EventArgs e)
        var bookslist = GetBooksList();
        int intBookCount = bookslist.Count();
        string strMaxPages = bookslist.Max(b => b.Pages).ToString();
        string strMinPages = bookslist.Min(b => b.Pages).ToString();
        string strAveragePages = bookslist.Average(b => b.Pages).ToString();
    public List<Books> GetBooksList()
        return new List<Books> {
                        new Books { Title="ASP.NET",ISBN="asp1",ReleaseDate= DateTime.Parse( "11/11/2010") ,Pages=200},
                        new Books { Title="C#.NET",ISBN="c#2",ReleaseDate= DateTime.Parse( "10/11/2010") ,Pages=500},
                        new Books { Title="VB.NET",ISBN="vb3",ReleaseDate= DateTime.Parse( "5/5/2009") ,Pages=400},
                        new Books { Title="SQL Server",ISBN="sql4",ReleaseDate= DateTime.Parse( "6/9/2010"),Pages=300 },
                        new Books { Title="JAVA",ISBN="java5",ReleaseDate= DateTime.Parse( "8/5/2011"),Pages=400 }

    } Example :
    Public Class Books
        Public Property Title() As String
        Public Property ISBN As String
        Public Property ReleaseDate As Date
        Public Property Pages As Integer
    End Class
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim bookslist = GetBooksList()
        Dim intBookCount As Integer = bookslist.Count()
        Dim strMaxPages As String = bookslist.Max(Function(b) b.Pages).ToString()
        Dim strMinPages As String = bookslist.Min(Function(b) b.Pages).ToString()
        Dim strAveragePages As String = bookslist.Average(Function(b) b.Pages).ToString()
    End Sub
    Public Function GetBooksList() As List(Of Books)
        Dim lstBooks As New List(Of Books) From { _
                                New Books With {.Title = "ASP.NET", .ISBN = "asp1", .ReleaseDate = DateTime.Parse("11/11/2010"), .Pages = 200}, _
                                New Books With {.Title = "C#.NET", .ISBN = "c#2", .ReleaseDate = DateTime.Parse("10/11/2010"), .Pages = 500}, _
                                New Books With {.Title = "VB.NET", .ISBN = "vb3", .ReleaseDate = DateTime.Parse("5/5/2009"), .Pages = 400}, _
                                New Books With {.Title = "SQL Server", .ISBN = "sql4", .ReleaseDate = DateTime.Parse("6/9/2010"), .Pages = 300}, _
                                New Books With {.Title = "JAVA", .ISBN = "java5", .ReleaseDate = DateTime.Parse("8/5/2011"), .Pages = 400}}

        Return lstBooks
    End Function


  1. To tell you the teuth I was passing around and come across your site. It is wonderful. I mean as a content and design. I added you to my list and decided to spent the rest of the weekend browsing. Well done!

  2. The post was able to express what it wants to convey to the readers. It has been a very effective approach which resulted to a profitable output for all who have been fortunate enough to come across it!
    business information

  3. This is such an informative article and very clearly written. Every single thought and idea is direct to the point. Perfectly laid out. Thank you for taking your time sharing this to you readers.
    sales force

  4. This comment has been removed by a blog administrator.

  5. This comment has been removed by a blog administrator.

  6. This comment has been removed by a blog administrator.

  7. This comment has been removed by a blog administrator.