Tuesday 4 September 2012

Beginning .Net , C# Tips : Convert from IEnumerable<> to Dictionary object using LINQ with C# Examples and VB.Net Examples

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.



2 comments: