Showing posts with label LambdaExpression. Show all posts
Showing posts with label LambdaExpression. Show all posts

Tuesday, 4 September 2012

Click Here to Download Sample ConvertIEnumerableToDictionary.zip

There is a situations where you need to convert LINQ result or IEnumerable<> object into Dictionary object. This is possible with the help of the LINQ Extension methods.

Here is example for this.
In this example we convert One LINQ's IEnumerable<> result in to Dictionary object. We take one books list object quering that object using LINQ and we converted it's LINQ result into Dictionary object. This dictionary object has string has key and book class object has value. As a key we take Book Title and value as book class object. After conversion we iterate dictionary object and display book name and book pages in list.

C# Examples :
    public class Books
    {
        public string Title { get; set; }
        public string ISBN { get; set; }
        public DateTime ReleaseDate { get; set; }
        public int Pages { get; set; }
        public int PublisherId { get; set; }
    }
    public List<Books> GetBooksList()
    {
        return new List<Books> {
                        new Books { Title="ASP.NET",ISBN="asp1",ReleaseDate= DateTime.Parse( "11/11/2010") ,Pages=200,PublisherId=1},
                        new Books { Title="C#.NET",ISBN="c#2",ReleaseDate= DateTime.Parse( "10/11/2010") ,Pages=500,PublisherId=1},
                        new Books { Title="VB.NET",ISBN="vb3",ReleaseDate= DateTime.Parse( "5/5/2009") ,Pages=400,PublisherId=1},
                        new Books { Title="SQL Server",ISBN="sql4",ReleaseDate= DateTime.Parse( "6/9/2010"),Pages=300,PublisherId=2 },
                        new Books { Title="JAVA",ISBN="java5",ReleaseDate= DateTime.Parse( "8/5/2011"),Pages=400,PublisherId=3 },
                        new Books { Title="HTML",ISBN="html6",ReleaseDate= DateTime.Parse( "9/5/2011"),Pages=400 }
        
        };

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        var bookslist = GetBooksList();
        
        var query = from bl in bookslist
                    select bl;

        Dictionary<string, Books> objDictionary = query.ToDictionary(aa => aa.Title);

        foreach (KeyValuePair<string, Books> bl in objDictionary)
        {
            Response.Write("<b>" + bl.Key + " : </b>" + bl.Value.Pages + "<br/>");

        }
    }

VB.net Examples :
    Public Class Books
        Public Property Title() As String
        Public Property ISBN As String
        Public Property ReleaseDate As Date
        Public Property Pages As Integer
        Public Property PublisherId As Integer
    End Class
    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, .PublisherId = 1}, _
                                New Books With {.Title = "C#.NET", .ISBN = "c#2", .ReleaseDate = DateTime.Parse("10/11/2010"), .Pages = 500, .PublisherId = 1}, _
                                New Books With {.Title = "VB.NET", .ISBN = "vb3", .ReleaseDate = DateTime.Parse("5/5/2009"), .Pages = 400, .PublisherId = 1}, _
                                New Books With {.Title = "SQL Server", .ISBN = "sql4", .ReleaseDate = DateTime.Parse("6/9/2010"), .Pages = 300, .PublisherId = 2}, _
                                New Books With {.Title = "JAVA", .ISBN = "java5", .ReleaseDate = DateTime.Parse("8/5/2011"), .Pages = 400, .PublisherId = 3}, _
                                New Books With {.Title = "HTML", .ISBN = "html6", .ReleaseDate = DateTime.Parse("9/5/2011"), .Pages = 400}}

        Return lstBooks
    End Function
    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 objDictionary As Dictionary(Of String, Books) = query.ToDictionary(Function(b) b.Title)

        For Each bl As KeyValuePair(Of String, Books) In objDictionary
            Response.Write("<b>" & bl.Key & " : </b>" & bl.Value.Pages & "<br/>")
        Next
    End Sub

Output :

For Beginning .Net articles. Click Here...

View More LINQ Examples. Click Here... 

This type of C# Tips is very useful in day to day programming life.

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.



Saturday, 2 June 2012

You can also query the array variables using LINQ.
Here are sample example for this.
In this example there is an array of integer values which contains various number, Now we want to get numbers in array which is greater then 5.
We can do this without FOR loop.

There are two possible way to do this. One is using LINQ Query and Second is Using LAMBDA Expression


C# Example using LINQ Query :
int[] arrayNumbers = new[] { 1,2,5,9,10,1,0,9,5,6,4,3,2};
int[] selectedNumber = (from an in arrayNumbers where an > 5 select an).ToArray();

C# Example using LAMBDA Expression:
int[] arrayNumbers = new[] { 1,2,5,9,10,1,0,9,5,6,4,3,2};
int[] selectedNumberLambda = arrayNumbers.Where(an => an > 5).ToArray();