0

Here is the structure:

Entity Diagram

And below is the code:

public IQueryable<PageTemplate> GetTemplate()
{
  var PageTemplates = from oPT in _db.PageTemplates
                      join oHSFK in _db.HelperSites on oPT.SiteID equals oHSFK.SiteID into oHSChild
                      from oHS in oHSChild.DefaultIfEmpty()
                      join oHUFK in _db.HelperUsers on oPT.SiteID equals oHUFK.UserID into oHUChild
                      from oHU in oHUChild.DefaultIfEmpty()
                      where oPT.SiteID == ConfigDto.SiteDetails.SiteID || oPT.SiteID == null
                      select new
                      {
                        TemplateID = oPT.TemplateID,
                        TemplateName = oPT.TemplateName,
                        //SiteName = oHS.SiteName,
                        //UpdatedByName = oHU.UserFirstName + " " + oHU.UserLastName,
                        UpdatedDate = oPT.UpdatedDate
                      };
  return null;
}

How do I return IQueryable<PageTemplate> which has related Entities already. I know the workaround of making a new class having all the required properties of PageTemplate, HelperSite & HelperUser classes. But, I am looking for a solution, if possible, to use existing Entity Framework classes.

iMatoria
  • 1,450
  • 2
  • 19
  • 35
  • The power of EF is you don't need JOIN - use your navigation properties. Then something like `var PageTemplates = _db.PageTemplates.Include(p => p.HelperSite).Include(p => p.HelperUser).Where(p => p.SiteID == ConfigDto.SiteDetails.SiteID || p.SiteID == null);` is possible. – Steve Greene Dec 20 '18 at 14:52
  • 1
    @SteveGreene: yea,I agree. I use the JOIN format because it is very close to SQL Queries and hence I get the correct logic. Thanks anyway. – iMatoria Dec 20 '18 at 15:03
  • I believe the problem is in the way you are writing your select at the end of your linq query. Take a close look at the structure of the select in the answer to this question: https://stackoverflow.com/questions/5348363/how-to-do-a-join-in-entity-framework – jdnew18 Dec 20 '18 at 15:33
  • @jdnew18: That is just another way of writing the code. End result will be same. Have a look at: https://stackoverflow.com/a/2767947/279755 – iMatoria Dec 21 '18 at 02:11
  • @iMatoria: Yes, you are using the query expression syntax while the sample I suggested is using the method expression syntax. As you know, they both work the same. What I wanted to bring attention to is that in my cited example (and yours), they are returning an anonymous object with multiple *class objects* in the select, while you are returning an anonymous object that just has *some columns*. Are you asking for a way to return multiple class objects? So something like: "select new PageTemplate { ...allYourPageTemplateFields..., HelperSites = oHS, HelperUser = oHU };"? – jdnew18 Dec 27 '18 at 16:07

0 Answers0