多个外部Linq连接

时间:2014-03-05 17:33:10

标签: linq asp.net-mvc-4

我有一个MVC应用程序,在其中我尝试通过加入来从多个表中拉取记录。在应用程序中,我有一个动物表,一张分娩表和一张购买表。每只动物都会在动物表中有记录,但只有购买进入农场的动物才会在购买表中记录,就像只有在农场出生的动物才能在出生表中记录一样。因此,对于我在下面的查询,出生表或购买表将始终返回null:

    CowDetailVM animal = (from animals in db.Animals
                              join breed in db.Breeds on animals.AnimalBreed equals breed.id
                              join birth in db.Births on animals.TagNo equals birth.TagNo
                              join purchase in db.Purchases on animals.TagNo equals purchase.TagNo
                              where animals.TagNo == id && animals.UserId == WebSecurity.CurrentUserId
                              orderby animals.DateAdded descending

                              select new CowDetailVM
                              {
                                  TagNo = animals.TagNo,
                                  Sex = animals.Sex,
                                  AnimalBreed = breed.Breed1,
                                  DOB = animals.DOB,
                                  OwnershipStatus = animals.OwnershipStatus,
                                  BornOnFarm = animals.BornOnFarm,

                                  /*DateBought = purchase.DateBought,
                                  BoughtFrom = purchase.BoughtFrom,
                                  Price = purchase.Price,
                                  Location = purchase.Location,

                                  MotherTagNo = birth.MotherTagNo,
                                  SireTagNo = birth.SireTagNo,
                                  Difficult = birth.Difficult*/

                              }).FirstOrDefault();

即使一个表返回null,我如何选择数据。或者有更好的方法来做到这一点?

由于

1 个答案:

答案 0 :(得分:0)

你几乎得到了它。您的查询会转换为INNER JOIN。要在LEFT JOINbirth表格上执行purchase,您可以执行以下操作。

CowDetailVM animal = (from animals in db.Animals
                      join breed in db.Breeds on animals.AnimalBreed equals breed.id
                      join birth in db.Births on animals.TagNo equals birth.TagNo into j0
                      from birth in j0.DefaultIfEmpty()
                      join purchase in db.Purchases on animals.TagNo equals purchase.TagNo into j1
                      from purchase in j1.DefaultIfEmpty()
                      where animals.TagNo == id && animals.UserId == WebSecurity.CurrentUserId
                      orderby animals.DateAdded descending

                      select new CowDetailVM
                      {
                          TagNo = animals.TagNo,
                          Sex = animals.Sex,
                          AnimalBreed = breed.Breed1,
                          DOB = animals.DOB,
                          OwnershipStatus = animals.OwnershipStatus,
                          BornOnFarm = animals.BornOnFarm,

                          /*DateBought = purchase.DateBought,
                          BoughtFrom = purchase.BoughtFrom,
                          Price = purchase.Price,
                          Location = purchase.Location,

                          MotherTagNo = birth.MotherTagNo,
                          SireTagNo = birth.SireTagNo,
                          Difficult = birth.Difficult*/

                      }).FirstOrDefault();