Click here to Skip to main content
15,909,498 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I want to get multiple results sets from LINQ query(with Entity Framework 4.1).

As i did one sample example which is working but i think this is not a proper way.
C#
var promotionProductObject = (from p in _dataEntities.CMS_Promotion
                              join pa in _dataEntities.CMS_ZO_PromotionArticle
                              on p.autPromotionId equals pa.intPromotionId
                              where (pa.intDeletedBy == null) && (pa.datDeletedOn == null) && (p.autPromotionId == 14)
                                      select new
                                      {
                                          p,
                                          productlist = from a in _dataEntities.Artikels
                                                        where pa.decArticleId == a.ID
                                                        select a
                                      }).ToList();

In the above example I am retrieving the value from promotion table and article table and the relation between them is promotion 1==>M promotionArtilce 1==>M article
i.e. i want all the list of article from the table based on promotinId eg 1004.
C#
for (int i = 0; i < promotionProductObject.Count(); i++)
        {
            productList1 = promotionProductObject.ToList()[i].productlist.ToList();
            productList.Add(productList1);
        }
        gvArticleDetails.DataSource = productList.ToList();
        gvArticleDetails.DataBind();

// in RowDataBound event
lblArticleNumber.Text = GetLocalResourceObject("lblArticleNumber") + productList.ToList()[_columnCount].ToList()[0].ArtikelNummer;

In this case when I bind the value with grid for article, I cant use the eval property, so alternately I have to use row databound event (which I dont want to use).

Is there any another good way to work on multiple result set with Linq query

Thank for support.
Posted
Updated 13-Nov-11 20:48pm
v3
Comments
[no name] 14-Nov-11 3:06am    
Are you expecting to get something equivalent to having multiple datatables within a dataset?

LOGIC IS MAGIC
Solved by My Self: Solution.

There are two methods i have found, by which we can bind the data with Eval in gridview.
C#
List articleList = new List();
    
    List articleList1 = new List();
#region Method I
        var promotionProductObject = (from p in _dataEntities.Promotions
                                      join pa in _dataEntities.PromotionArticles
                                      on p.PromotionId equals pa.PromotionId
                                      where (pa.DeletedBy == null) && (pa.DeletedOn == null) && (p.PromotionId == 14)
                                      select new
                                      {
                                          p,
                                          productlist = (from a in _dataEntities.Artikels
                                                         where pa.ArticleId == a.ID
                                                         select a)
                                      }).ToList();

        if (promotionProductObject != null)
        {
            for (int i = 0; i < promotionProductObject.Count(); i++)
            {
                Artikel article = promotionProductObject[i].productlist.ToList()[0];
                articleList.Add(article);
            }

            grdArticleDetails.DataSource = articleList;
            grdArticleDetails.DataBind();
        }

        #endregion Method I

        #region Method II
        var promotionArtilceObject = (from p in _dataEntities.Promotions
                                      join pa in _dataEntities.PromotionArticles
                                      on p.PromotionId equals pa.PromotionId
                                      join a in _dataEntities.Artikels
                                      on pa.ArticleId equals a.ID
                                      where (pa.DeletedBy == null) 
                                      && (pa.DeletedOn == null) && (p.PromotionId == 14)
                                      select new
                                      {
                                          p,//Promotion
                                          productList = a // Article
                                      }).ToList();

        if (promotionArtilceObject != null)
        {
            for (int i = 0; i < promotionArtilceObject.Count(); i++)
            {
                articleList1.Add(promotionArtilceObject[i].productList);
            }
            grdArticleDetails1.DataSource = articleList1;
            grdArticleDetails1.DataBind();
        }
        #endregion Method II
    }


And the value can be access by -
lblArticleName.Text = articleList1[_rowCount1].Bezeichnung;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900