Wednesday 23 May 2012

C# Programming : Simple filter query with LINQ To Objects

You always need to filter the records based on certain fileds.
Using LINQ query you are able to get this.

Here are sample example for this.
In this example We add where clause in LINQ query to get filtered records, Here we wants only records whose ISBN Number is "asp1".

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();
        //var query = from bl in bookslist select bl;
        var query = from bl in bookslist
                    where bl.ISBN=="asp1"
                    select new {FullTitle= bl.Title,ISBNNumber=bl.ISBN };
        this.gvBooks.DataSource = query;
        this.gvBooks.DataBind();
    }
    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 }
        
        };

    }
VB.net 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 query = From bl In bookslist _
        '            Select bl
        Dim query = From bl In bookslist _
                    Where bl.ISBN = "asp1" _
                    Select New With {.FullTitle = bl.Title, .ISBNNumber = bl.ISBN}
        gvBooks.DataSource = query
        gvBooks.DataBind()
    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

4 comments:

  1. very nice. thanks. easy to learn. can you provide some more example of LINQ with other source like xml, databases etc.

    ReplyDelete
  2. i want to ask you that how can i subscribe to your blog so that i can get all your new stuff directly to my mail.

    ReplyDelete
    Replies
    1. Hi,
      Thanks for your valuable comments.
      More articles on LINQ and LINQ With XML is coming soon.
      You can Use RSS feed for daily updates.
      This is http://jayeshsorathia.blogspot.com/rss.xml RSS URL pf this blog.
      You can also use "Follow by Email" section of this blog.

      Delete