多个左连接LINQ到实体

时间:2010-10-04 21:15:38

标签: c# linq-to-entities left-join

我有3张桌子:

Dealerships
------------
ID, Name, Website

Locations
------------
ID, DealershipID, Address, Ect.

Contacts
------------
ID, LocationID, Name, Ect.

因此,这种关系表明我们拥有多个地点的经销商(例如:PA的Weed Chevrolet,新泽西州的Weed Chevrolet),然后每个地点都有自己的联系人(例如:PA位置经理,NJ位置经理)。我需要一起加入3张桌子。这就是我所拥有的:

var results = from d in entities.dealerships
              join l in entities.locations on d.ID equals l.DealershipID
              join c in entities.contacts on l.ID equals c.LocationID
              select new
              {
                  Name = d.Name,
                  Website = d.Website,
                  Address = l.Address + ", " + l.City + ", " + l.State + " " + l.Zip,
                  Contact = c.FirstName + " " + c.LastName,
                  WorkPhone = c.WorkPhone,
                  CellPhone = c.CellPhone,
                  HomePhone = c.HomePhone,
                  Email = c.Email,
                  AltEmail = c.AltEmail,
                  Sells = l.Sells
               }

当我尝试将结果绑定到BindingSource然后绑定到DataGridView时,我收到以下错误:

Unable to cast the type 'System.Nullable`1' to type 'System.Object'.
LINQ to Entities only supports casting Entity Data Model primitive types.

它能是什么?我是LINQ中JOIN语句的新手,所以我确信我做错了。

编辑:数据库中有数据,因此结果不应为空,只是为了澄清

3 个答案:

答案 0 :(得分:12)

你很接近,但我发现你必须将它从LINQ-To-Entities转换为LINQ-To-Objects。首先,我必须使用AsEnumerable()投射实体,然后使用ToList()。这样就可以使用ToString()String.Format()等功能。谢谢你带领我朝着正确的方向前进。这是最终的代码:

var query = from d in entities.dealerships
            from l in entities.locations.Where(loc => loc.DealershipID == d.ID).DefaultIfEmpty()
            from c in entities.contacts.Where(cont => cont.LocationID == l.ID).DefaultIfEmpty()
            where d.Keywords.Contains(keywords) || l.Keywords.Contains(keywords) || l.Sells.Contains(keywords) || c.Keywords.Contains(keywords)
            select new
            {
                Dealership = d,
                Location = l,
                Contact = c
            };

var results = (from r in query.AsEnumerable()
               select new
               {
                   Name = r.Dealership.Name,
                   Website = r.Dealership.Website,
                   Contact = r.Contact.FirstName + " " + r.Contact.LastName,
                   Address = r.Location.Address + ", " + r.Location.City + ", " + r.Location.State + " " + r.Location.Zip,
                   WorkPhone = r.Contact.WorkPhone,
                   CellPhone = r.Contact.CellPhone,
                   Fax = r.Contact.Fax,
                   Email = r.Contact.Email,
                   AltEmail = r.Contact.AltEmail,
                   Sells = r.Location.Sells
               }).ToList();

bindingSource.DataSource = results;

答案 1 :(得分:2)

由于您的结果是IQueryable,因此EF会尝试在数据存储方面进行强制转换,但它不起作用,因为强制转换仅适用于标量类型。您应该在结果上调用ToList(),如下所示:

var results = (from d in entities.dealerships
              join l in entities.locations on d.ID equals l.DealershipID
              join c in entities.contacts on l.ID equals c.LocationID
              select new
              {
                  Name = d.Name,
                  Website = d.Website,
                  Address = l.Address + ", " + l.City + ", " + l.State + " " + l.Zip,
                  Contact = c.FirstName + " " + c.LastName,
                  WorkPhone = c.WorkPhone,
                  CellPhone = c.CellPhone,
                  HomePhone = c.HomePhone,
                  Email = c.Email,
                  AltEmail = c.AltEmail,
                  Sells = l.Sells
               }).ToList();

答案 2 :(得分:0)

var EmplistDriver = (from a in data
                     join b in db.DesignationDetails on a.DesignationID equals b.DesignationDetailID into EmployeeBonus
                     from b in dataBonus.DefaultIfEmpty()

                     join x in db.EmployeeCommission on a.EmployeeDetailID equals x.EmployeeDetailID into EmployeeCommission
                     from x in dataComm.DefaultIfEmpty()

                     join c in db.EmployeeAdvance on a.EmployeeDetailID equals c.FKEAEmployeeID

                     join d in db.EmployeeAllowance on a.EmployeeAllowanceID equals d.EmployeeAllowanceID
                     join e in dataAtt on a.EmployeeDetailID equals e.EmployeeDetailID

                     join f in dataDri on a.EmployeeDetailID equals f.EmployeeDetailID
                     join h in db.ProjectAllocation on f.FKAllocationID equals h.PKAllocationID

                     join i in db.ProjectDetails on h.FKProjectDetailID equals i.ProjectDetailID


                     where a.IsActive == true && c.EAIsActive == true && d.IsActive == true && e.EAIsActive == true && h.IsActivity == true

                     select new
                     {
                         c.BalanceAmount,
                         c.BalanceDue,
                         d.FoodAllowance,
                         i.DriverBasicSalary,
                         d.OtherAllowance,
                         d.AccommodationAllowance,
                         e.EABasicWorktime,
                         BonusAmount = (b.BonusAmount == null ? 0 : b.BonusAmount),
                         CommissionAmount = (x.CommissionAmount == null ? 0 : x.CommissionAmount),
                         TotalOverTime,
                         TotalHr

                     }).FirstOrDefault();