none
How to display data hierarchically from db with EF code first RRS feed

  • Question

  • I have to fetch data from DB table with EF code first. I want to show hierarchical data in for loop. sample data the way I want to show as follows

    2015
        JAN
            title1 hyperlink
            title2 hyperlink    
        FEB
            title3 hyperlink    
        MARCH
            title4 hyperlink    
    
        .....
    
        DECEMBER
            title15 hyperlink       
    
    2016
        JAN
            title11 hyperlink
            title12 hyperlink   
        FEB
            title13 hyperlink   
        MARCH
            title14 hyperlink   
        .....
    
        DECEMBER
            title15 hyperlink   

    just i have concern that how my c# class would look like for showing data in that fashion and also how to write code to show data.

    anyone can post sample code with class and sample data populated. thanks

    Friday, September 23, 2016 10:41 AM

Answers

  • Something like this?

    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication24
    {
        class Article
        {
            public int Id { get; set; }
            public DateTime PublicationDate { get; set; }
            public string Title { get; set; }
            public string Url { get; set; }
        }
        class ArticleDb: DbContext
        {
            public DbSet<Article> Articles { get; set; }
        }
    
        class Program
        {
    
            static void Main(string[] args)
            {
                Database.SetInitializer(new DropCreateDatabaseAlways<ArticleDb>());
    
                using (var db = new ArticleDb())
                {
    
                    var q1= from year in Enumerable.Range(2010, 5)
                             from month in Enumerable.Range(1, 12)
                             from articleNumber in Enumerable.Range(1, 1+ ((year * month * 7) % 11))
                             select new { year, month, articleNumber, publicationDayOfMonth = 1+ (articleNumber * year * 11) % 20};
    
                    var l = q1.ToList();
                    foreach (var a in l)
                    {
                        var article = db.Articles.Create();
                        article.PublicationDate = new DateTime(a.year, a.month, a.publicationDayOfMonth);
                        article.Title = $"Article {a.articleNumber}";
                        article.Url = $"http://mysite/{a.year}/{a.month}/{a.articleNumber}";
                        db.Articles.Add(article);
                    }
                    db.SaveChanges();
    
                    var q = from a in db.Articles.ToList()
                            orderby a.PublicationDate
                            group a by new { a.PublicationDate.Year, a.PublicationDate.Month } into byMonth
                            orderby byMonth.Key.Month, byMonth.Key.Year
                            group byMonth by byMonth.Key.Year into byYear
                            orderby byYear.Key
                            select byYear;
    
                    var articles = q.ToList();
                    foreach (var year in articles)
                    {
                        Console.WriteLine(year.Key);
                        foreach (var month in year)
                        {
                            Console.WriteLine("  " + month.Key);
                            foreach (var article in month)
                            {
                                Console.WriteLine($"         {article.Title} {article.Url}");
    
                            }
                        }
                    }       
                }
            }
        }
    }
    

    David


    David http://blogs.msdn.com/b/dbrowne/

    • Marked as answer by Sudip_inn Saturday, September 24, 2016 8:18 AM
    Friday, September 23, 2016 3:01 PM