devlin carnate
devlin carnate

Reputation: 8592

How to join two tables in linq and not get anonymous type result set

I would like to get a result set from both PriceAssocationLookup and PriceAssociation tables. How do I do this and not get an error due to Anonymous Type? Here is my code:

IEnumerable<IPriceAssociationLookupRepository> IPriceAssociationLookupRepository.GetPacs(string upc)
{
    using (PortalDataEntities entities = new PortalDataEntities())
    {
        var priceAssociationLookups = (from priceassociationlookup in entities.PriceAssociationLookups
                                       join priceassociation in entities.PriceAssociations on priceassociationlookup.PriceAssociationCode equals priceassociation.PriceAssociationCode
                                       where priceassociationlookup.Upc == upc
                                       select priceassociationlookup ).ToList();

        return priceAssociationLookups;
    }

}

Upvotes: 0

Views: 1049

Answers (1)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

Create a ViewModel and add properties for the columns you want to return and return List of the view model type, here is my code, the way i used to do :

List<PreviousTest> Result = (from d in db.dc_tpatient_bookingd

                            join og in db.dc_tp_organization 
                            on d.clientid equals og.OrgId into a

                            from og in a.DefaultIfEmpty()

                            from t in db.dc_tp_test

                            from p in db.dc_tp_tprocess

                            where d.bookingid == BookingID

                            && t.TestId == d.testid

                            && d.ProcessID == p.processid

                            && d.bookingdid != BookingDID

                            select new PreviousTest
                            {
                             BookingID = d.bookingid,
                             BookingDId = d.bookingdid,
                             TestID = t.TestId,
                             TestName = t.Test_Name,
                             ProcessName = p.name,
                             ProcessID = p.processid,
                             ClientID =  d.clientid
                             }).ToList();

Here is my viewmodel :

public class PreviousTest
    {

        public long BookingID { get; set; }
        public long BookingDId { get; set; }
        public long TestID { get; set; }
        public string TestName { get; set; }
        public long ProcessID { get; set; }
        public string ProcessName { get; set; }
        public string ClientID { get; set; }

    }

Upvotes: 2

Related Questions