简体   繁体   English

Linq 查询以包括连接集合的项目数

[英]Linq query to include number of items of a joined collection

I have three tables in DB我在数据库中有三个表

  • Person: [PersonId, Name, AddressId]人员:[PersonId,姓名,AddressId]
  • Address: [AddressId, CityName]地址:[AddressId, CityName]
  • UploadPhoto: [PhotoId, PersonId]上传照片:[PhotoId,PersonId]

Assume one person has one address, but can have multiple uploaded photo.假设一个人有一个地址,但可以上传多张照片。 I want to write a Linq query to produce a joined collection with item like我想编写一个 Linq 查询来生成一个包含类似项目的连接集合

{
   PersonId: 5;
   Name: "Jack";
   CityName: "Edmonton";
   NumOfUploadPhoto: 10;
}

What's the Linq query looks like? Linq 查询是什么样的? Thanks谢谢

So you have a one-to-many between Addresses and Persons: Every Address has zero or more Persons, every Person lives at exactly one Address, namely the Address that the foreign key Person.AddressId refers to.所以你在 Addresses 和 Persons 之间有一个一对多的关系:每个 Address 都有零个或多个 Persons,每个 Person 都生活在一个 Address 上,即外键 Person.AddressId 所指的 Address。

Similarly you have a one-to-many relation between Persons and Photos: every Person has uploaded zero or more Photos, every Photo has been uploaded by the Person that the foreign key Photo.PersonId refers to.同样,Persons 和 Photos 之间存在一对多的关系:每个 Person 都上传了零个或多个 Photos,每张 Photo 都由外键 Photo.PersonId 所指的 Person 上传。

Whenever you wnat "items with their zero or more sub-items", like Schools with their Students, Customers with their Orders, or in your case: Persons with their uploaded Photos, consider to use Enumerable.GroupJoin .每当您拥有“带有零个或多个子项目的项目”时,例如学校及其学生,客户及其订单,或者在您的情况下:上传照片的人,请考虑使用Enumerable.GroupJoin

The other way round: if you have an item and you want the one and only item that the foreign key refers to: the Student with the School that he attends, the Order with the Customer who made the order, or in your case: the Person with the City that he lives in, consider to use Enumerable.Join反过来:如果您有一个项目,并且您想要外键引用的唯一一个项目:学生与他就读的学校,与下订单的客户的订单,或者在您的情况下:与他居住的城市的人,考虑使用Enumerable.Join

In a one-to-many, if you want the one item with the many sub-items start at the one-side and use GroupJoin.在一对多中,如果您希望具有许多子项的一项从一侧开始并使用 GroupJoin。 If you want the sub-item with the one item that the foreign key refers to, start at the many-side and use Join.如果您想要具有外键引用的一项的子项,请从多方开始并使用 Join。

You want the Person with the many photos and the one address that the foreign key refers to.您想要拥有许多照片的 Person 和外键引用的一个地址。

var person = dbContext.Persons.GroupJoin(dbContext.UploadedPhotos,

person => person.PersonId,        // from every Person take the primary key
photo => photo.PersonId,          // from ever Photo take the foreign key to the Person

// parameter resultSelector: from every Person, with its zero or more Photos, make one new:
(person, photosOfThisPerson) => new
{
    PersonId = person.PersonId,
    Name = person.Name,
    NumberOfUploadedPhotos = photosOfThisPerson.Count(),

    Address = dbContext.Addresses.Where(address => address.AddressId == person.AddressId)
        FirstOrDefault(),
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM