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

Retrieve multiple values using join in LINQ

$
0
0

I have a problem with the following query as I want to get the record from multiple tables where PostId is same for this purpose I use join and this works perfectly but the problem is in PostMeta table as it save multiple record for one PostId and I wan to to get all the records for each postId. How do I get that

here is my query

 var alltender = (from q in _sepiacontext.Post
                             join k in _sepiacontext.PostRef
                             on q.PostId equals k.PostId                          
                             join d in _sepiacontext.AppUsers
                             on q.PostId equals d.CreatedBy
                             join cc in _sepiacontext.PqaDivision
                             on d.DivisionId equals cc.DivisionId
                             join dep in _sepiacontext.AppDepartment
                             on d.DepartmentId equals dep.DepartmentId
                             where q.PostTypeId == 2 && k.LangId == 1 /*&& p.TextVal != null*/
                             select new PartyTenderVM
                             {

                                 TenderName = k.PostTitle,
                                 Content = k.PostContent,
                                 isSubcribe = false,
                                 TenderFile = q.PostMeta.FirstOrDefault(q => q.PostTypeMetaId == 12).TextVal,
                                 PostId = q.PostId,
                                 PostParentId = q.PostParentId,
                                 OpeningDate =q.PostMeta.FirstOrDefault(q=>q.PostTypeMetaId==14).TextVal,
                                 ClosingDate = q.PostMeta.FirstOrDefault(q => q.PostTypeMetaId == 15).TextVal,
                                 views = q.Views,
                                 DepartmentName=dep.AppDepartmentName,
                                 DivisionName=cc.DivisionName,

                             }).GroupBy(x => x.PostId).Select(y => y.FirstOrDefault()).OrderByDescending(q=>q.PostId).ToList();


Viewing all articles
Browse latest Browse all 1698

Trending Articles



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