MaxOvrdrv
MaxOvrdrv

Reputation: 1916

return combined entity from join

Is it possible to return a single object, that is comprised of a join between two entities using EF6?

Here is an example of what I mean, however, I do not know what type to use, select or return in order to get the combined data set... so I have denoted those with the use of ?????? within the example.

    public static ?????? GetAppDetails(AppsLight item)
    {
        using (ApplicationEntities db = new ApplicationEntities())
        {
            ?????? retval = (from t in db.Applications
                             join g in db.Licenses on t.LicenseID equals g.LicenseID
                             where t.AppID == item.UID
                             select ????).FirstOrDefault();

            return retval;

        }
    }

Upvotes: 0

Views: 126

Answers (2)

DLeh
DLeh

Reputation: 24395

You should create your own type to return.

public class MyReturnType
{
    public Application App { get; set; }
    public License License { get; set; }
}


public static MyReturnType GetAppDetails(AppsLight item)
{
    using (ApplicationEntities db = new ApplicationEntities())
    {
        var retval = (from t in db.Applications
                         join g in db.Licenses on t.LicenseID equals g.LicenseID
                         where t.AppID == item.UID
                         select new MyReturnType
                         {
                            App = t,
                            License = l
                         }).FirstOrDefault();
        return retVal;
    }
}

If you have properly configured foreign keys, you shouldn't need to do an explicit join, and can do something like this.

public static Application GetAppDetails(AppsLight item)
{
    using (ApplicationEntities db = new ApplicationEntities())
    {

        var application = (from t in db.Applications
                         where t.AppID == item.UID
                         select new MyReturnType)
             .Include(x => x.License)
             .FirstOrDefault();

        //application should have a .License property. 
        return application;
    }
}

If you don't want to create a new type you could also just use Tuple:

public static Tuple<Application, License> GetAppDetails(AppsLight item)
{
    using (ApplicationEntities db = new ApplicationEntities())
    {
        var tuple = (from t in db.Applications
                         join g in db.Licenses on t.LicenseID equals g.LicenseID
                         where t.AppID == item.UID
                         select Tuple.Create(t, l))
                     .FirstOrDefault();
        return tuple;
        //access the items in the tuple like this: tuple.Item1; tuple.Item2;
    }
}

Upvotes: 1

coder99
coder99

Reputation: 1110

  public static CombinedDto GetAppDetails(AppsLight item)
{
    using (ApplicationEntities db = new ApplicationEntities())
    {
        CombinedDto retval = (from t in db.Applications
                         join g in db.Licenses on t.LicenseID equals g.LicenseID
                         where t.AppID == item.UID
                         select(b=>new CombinedDto { ApplicationId = t, LicenceId = item})).FirstOrDefault();

        return retval;

    }
}
publi class CombinedDto()
{
  public Application Application {get;set;}
  public Licence Licence {get;set;}
}

Use a dto, or anonymus objects. i think it would be better. Thank you.

Upvotes: 0

Related Questions