Home  • Programming • ASP.NET

One to many mode relationship in C# ASP.Net core using Linq

select-name-cmbcountry From a single query
DataTable shops=query();
List<SellerApplicationModel> shoplist = (from i in shops.AsEnumerable()
                       select new SellerApplicationModel
                       {
                            Id = i.Field<Int64>("id"),
                            Name = i.Field<String>("business_name"),
                            Banners = new List<Banner>{ new Banner {
                                                     Title = i.Field<String>("banner_title"),
                                                     CssClass = i.Field<String>("banner_css_class"),
                                                     Slogan = i.Field<String>("slogan"),
                                                     OfferHead = i.Field<String>("offer_head"),
                                                     OfferBody = i.Field<String>("offer_body") 
                                                },
                                             new Banner {
                                                   Title = i.Field<String>("banner_title2"),
                                                   CssClass = i.Field<String>("banner_css_class2"),
                                                   Slogan = i.Field<String>("slogan2"),
                                                   OfferHead = i.Field<String>("offer_head2"),
                                                   OfferBody = i.Field<String>("offer_body2") 
                                                },

                                           }
                       }.ToList()
  
From Two Tables
DataTable shops=query();
DataTable banners=query();
List<SellerApplicationModel> shoplist = (from i in shops.AsEnumerable()
                       select new SellerApplicationModel
                       {
                            Id = i.Field<Int64>("id"),
                            Name = i.Field<String>("business_name"),
                            Banners = (from b in banners.AsEnumerable()
                               select new Banner{
                                  Title = i.Field<String>("banner_title"),
                                  CssClass = i.Field<String>("banner_css_class"),
                                  Slogan = i.Field<String>("slogan"),
                                  OfferHead = i.Field<String>("offer_head"),
                                  OfferBody = i.Field<String>("offer_body") 
                                }                                    
                            ).ToList<Banner>()
                       }.ToList()
  

Comments 0


Share