如何在linq中连接两个表而不是获取匿名类型结果集

时间:2014-04-07 15:43:34

标签: asp.net-mvc linq asp.net-mvc-4

我想从PriceAssocationLookup和PriceAssociation表中获取结果集。如何执行此操作而不会因匿名类型而出错? 这是我的代码:

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;
    }

}

1 个答案:

答案 0 :(得分:1)

为要返回的列创建 ViewModel 并添加属性,并返回视图模型类型的列表,这是我的代码,我以前的方式:

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();

这是我的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; }

    }