查询LINQ中一对多关系的对象

时间:2018-12-16 16:07:42

标签: entity-framework linq group-by linq-to-entities linq-to-objects

我遇到的问题似乎与
类似 Query objects for one to many relationship in LINQ

我一直在尝试解决上面提到的问题,但是以某种方式我失败了很长时间。

场景
我有一个名为Business的模型,该模型具有一个ICollection of Branches,并且该业务属于特定的 SubCategory

模型看起来像

public class SubCategory
{
    public string Id { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    public string ImageUrl { get; set; }

    public string Category_FK { get; set; }

    public Category Category { get; set; }

    [ForeignKey("SubCategory_FK")]
    public ICollection<Business> Businesses { get; set; }

    public SubCategory(){
        Businesses = new Collection<Business>();
    }
}

public class Business
{
    public string Id { get; set; }

    public string Name { get; set; }

    public string Email { get; set; }

    public string HeadquartersContact { get; set; }

    public string ImageUrl { get; set; }

    public SubCategory SubCategory { get; set; }

    public string SubCategory_FK { get; set; }
    [ForeignKey("Business_Fk")]
    public ICollection<Branch> Branches { get; set; }

    public Business()
    {
        Branches = new Collection<Branch>();
    }
}

分支模型看起来像

public class Branch
{
    public string Id { get; set; }

    public string Name { get; set; }

    public string Phone { get; set; }

    public string ImageUrl { get; set; }

    public Business Business { get; set; }

    public string Business_Fk { get; set; }

    [ForeignKey("Location_FK")]
    public Location Location { get; set; }

    public string Location_FK { get; set; }

    [ForeignKey("Branch_FK")]
    public ICollection<BranchImage> BranchImages { get; set; }

    public Branch()
    {
        BranchImages = new Collection<BranchImage>();
    }
}

每个分支可以有一个位置。所以下面是位置模型

public class Location
    {
        public string Id { get; set; }

        public string Name { get; set; }

        public decimal Lattitude { get; set; }

        public decimal Longitude { get; set; }

        public SubLocality SubLocality { get; set; }

        public string SubLocality_FK { get; set; }

        public Branch Branch { get; set; }

        public Location()
        {
        }
    }

一个子地区可以具有ICollection of Locations。这是SubLocality模型

public class SubLocality
{
    public string Id { get; set; }

    public string Name { get; set; }

    public Locality Locality { get; set; }

    public string Locality_FK { get; set; }

    [ForeignKey("SubLocality_FK")]
    public ICollection<Location> Locations { get; set; }

    public SubLocality()
    {
        Locations = new Collection<Location>();
    }


}

位置可以具有子位置的ICollection

public class Locality
{
    public string Id { get; set; }

    public string Name { get; set; }

    public City City { get; set; }

    public string City_FK { get; set; }
    [ForeignKey("Locality_FK")]
    public ICollection<SubLocality> SubLocalities { get; set; }

    public Locality()
    {
        SubLocalities = new Collection<SubLocality>();
    }
}

一个城市可以拥有ICollection of Localities。

public class City
{
    public string Id { get; set; }

    public string Name { get; set; }

    public State State { get; set; }

    public string State_FK { get; set; }
    [ForeignKey("City_FK")]
    public ICollection<Locality> Localities { get; set; }

    public City()
    {
        Localities = new Collection<Locality>();
    }
}

最后,一个州可以拥有城市的ICollection。

public class State
{
    public string Id { get; set; }

    public string Name { get; set; }

    [ForeignKey("State_FK")]
    public ICollection<City> Cities { get; set; }

    public State()
    {
        Cities = new Collection<City>();
    }
}

这是我的 DataContext ,我正在查询中使用其实例来获取数据。

public class DataContext : DbContext
{
    public DataContext(DbContextOptions<DataContext> options): base (options)
    {

    }

    //-------Business_Info_Models----------------
    public DbSet<Branch> Branches { get; set; }
    public DbSet<BranchImage> BranchImages { get; set; }
    public DbSet<Business> Businesses { get; set; }

    //-------Category_Info_Models----------------
    public DbSet<Category> Categories { get; set; }
    public DbSet<SubCategory> SubCategories { get; set; }

    //-------------Location_Info_Models----------------
    public DbSet<City> Cities { get; set; }
    public DbSet<Locality> Localities { get; set; }
    public DbSet<Location> Locations{ get; set; }
    public DbSet<State> States { get; set; }
    public DbSet<SubLocality> SubLocalities { get; set; }
}

要求
现在,我需要查询一个特定子类别的企业,并获取所有分支(例如,一个企业可以有8个分支,5个分支等)。它是位置,子位置,城市,州(要说的是,我通过使用LINQ中的join从不同的表中获得所有这些数据)。

为了满足此要求,我使用了DTO(数据传输对象)。

public class AddBusinessDto
{
    public string Id { get; set; }

    public string Name { get; set; }

    public string Email { get; set; }

    public string HeadquartersContact { get; set; }

    public string ImageUrl { get; set; }

    public string SubCategory_FK  { get; set; }

    public ICollection<AddBranchDto> Branches { get; set; }
    //public ICollection<AddLocationsDto> Locations {get; set; }
}

public class AddBranchDto
{
    public string Id { get; set; }

    public string Name { get; set; }

    public string Phone { get; set; }

    public string ImageUrl { get; set; }

    public AddLocationsDto Location { get; set; }
    public string Location_FK { get; set; }
}

LINQ查询

IQueryable<BusinessDto> query =  (from business in _context.Businesses
        join subcategory in _context.SubCategories on business.SubCategory_FK equals subcategory.Id
        join branch in _context.Branches on business.Id equals branch.Business_Fk 
        join location in _context.Locations on branch.Location_FK equals location.Id
        join sublocal in _context.SubLocalities on location.SubLocality_FK equals sublocal.Id
        join locality in _context.Localities on sublocal.Locality_FK equals locality.Id
        join city in _context.Cities on locality.City_FK equals city.Id
        join state in _context.States on city.State_FK equals state.Id
        where business.SubCategory_FK == subCatId
        select new BusinessDto() {
            Id = business.Id,
            Name = business.Name,
            HeadquartersContact = business.HeadquartersContact,
            Location = location.Name,
            Sublocality = sublocal.Name,
            Locality = locality.Name,
            City = city.Name,
            State = state.Name,
            Email = business.Email,

            Branches = business.Branches
                        .Select(branches => 
                        new BranchDto{Id = branches.Id, Name = branches.Name, Phone = branches.Phone})
                        .Distinct()
                        .ToList()
        });

我几乎可以得到结果,但是这个查询的 BUG 是:

The buggy output

上述业务在SS中有8个分支机构。理想情况下,当我将鼠标悬停在分支图标上时,我不需要在UI上只显示一张显示8个分支数据的卡片。

但是查询给了我8张这样的卡片。我该如何解决。请帮助!

0 个答案:

没有答案