Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

GroupJoin has flat results

$
0
0

Hello,

 im trying to query db (using EF core ) a table that has a child and the cild has a child , the results im getting are instead of "tree"  (x)1 father>3(x) sun>(x)1 grandson

im getting  father  3 times and the proper grandson for each son 

code: 

 public class QuettaReq
    {
        public QuettaReq()
        {
        }

        [Key] public int             Id         { get; set; }
        public       ApplicationUser User       { get; set; }
       //Properties 
        public virtual List<QuoteQuestion>  QuoteQuestions { get; set; }
}
 public class QuoteQuestion
    {
        [Key]
        public int QuoteQuestionId { get; set; }
        public int QuoteId         { get; set; }
        public QuettaReq QuettaReq { get; set; }

        public string Question { get; set; }
        public IList<Answers> Answers { get; set; }

    }
 public class Answers
    {
        public int AnswersId { get; set; }
        public int QuoteQuestionId { get; set; }
        public string Answer { get; set; }

    }

ViewModels

 public class ReqestWithQA
    {
        [Key] public int Id { get; set; }
       //Properties

        public virtual IList<QAViewModel>  QAViewModel { get; set; }
        //public virtual IList<AnsweToSeller> AToBuyer { get; set; }
        public int    QuettaReqId { get; set; }
        public string Question    { get; set; }
    }
 public class QAViewModel
    {
        public int QuoteQuestionId { get; set; }
        public int QuoteId { get; set; }
        public string Question { get; set; }

        public IList<Answers> Answers { get; set; }
        }

Controller 

   IQueryable<ReqestWithQA>
                            viewModel =   _context.Quetta.Include(q => q.Category)
                                .Where(d => d.OfferDate > DateTime.Now && d.CatId == suplayerCat)
                                .Where(rq => !postOn.Contains(rq.Id)).Distinct()  
                                .GroupJoin(
                                    _context.quoteQuestions.Include(t =>
                                        t.Answers),
                                    quetta => quetta.Id,
                                    qestion => qestion.QuoteId, (quetta, joinQestionQuetta) => new ReqestWithQA
                                    {
                                        ReqText         = quetta.ReqText,
                                        Qentity         = quetta.Qentity,
                                        CatId           = quetta.CatId,
                                        CatName         = quetta.Category.CatName,
                                        District        = quetta.District,
                                        ApplicationDate = quetta.ApplicationDate,
                                        DeliveryDate    = quetta.DeliveryDate,
                                        OfferDate       = quetta.OfferDate,
                                        TecnicalDetails = quetta.TecnicalDetails,
                                        Bedget          = quetta.Bedget,
                                        Id              = quetta.Id,
                                        QAViewModel = new[]
                                        {
                                            joinQestionQuetta
                                                .Select(q => new QAViewModel
                                                {
                                                    Question = q.Question,
                                                    Answers  = q.Answers,
                                                    QuoteId  = q.QuoteId
                                                }).FirstOrDefault()
                                        }
                                    });// should be one quetta with 3 question & one question answer  instead 3 results each has one question .. 

Thanks in advance 


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>