简体   繁体   中英

Complex Query LINQ Lambda

I am trying to extract data in a single query. Since it involves many tables, I am sort of stuck at the grouping part.

I do not have enough reputation to post image of my table design. So, I am giving PK,FK

 Sector (SectorId)
 Device (DeviceId:PK, CategoryId:FK)
 Ratio (SectorId,DeviceId)
 Use (UseId)
 DeviceUse (SectorId,DeviceId,UseId)
 Family (FamilyId)
 Category (CategoryId)
 Level (LevelId)
 Age(AgeId)
 Consu (SectorId,DeviceId,LevelId)
 DistributionOne (SectorId,DeviceId,LevelId)
 DistributionTwo (SectorId,DeviceId,LevelId, AgeId)

What I am trying to achieve is:

Given a sectorId , retrieve all related information from all the tables given.

The result would be:

all Devices

grouped by Family

grouped by Category

and all Ratios (for given sectorId and deviceId )

and all DeviceUses (for related sectorId and deviceId ) and the related Use for the deviceId

and all Consu (for related deviceId , levelId , ageId ) and the related Age and Level

and all DistributionOne (for related deviceId , levelId , sectorId ) and the related Level

and all DistributionTwo (for related deviceId , levelId , sectorId , ageId ) and the related Age and Level

So far I got a method as below.

public IEnumerable<UserConfig> GetDeviceType(int sectorId)
    {
        var t = repo.GetAll().AsQueryable()                
            .Select(
            c => new UserConfig
            {
                Device = new Device { Name = c.Name, Id = c.Id },
                DistributionOne = c.DistributionOne.Where(d => d.SectorId == sectorId && d.DeviceId == c.Id).ToList(),
                DistributionTwo = c.DistributionTwo.Where(d => d.SectorId == sectorId && d.DeviceId == c.Id).ToList(),
                Consu = c.Consu.Where(d=>d.DeviceId == c.Id).ToList(),
                Category = c.Category,
                Family = c.Category.Family,
                DeviceUse = c.DeviceUse.Where(d => d.SectorId == sectorId && d.DeviceId == c.Id).ToList(),
                Ratios = c.Ratios.Where(d => d.SectorId == sectorId && d.DeviceId == c.Id).ToList(),
                Use = c.DeviceUse.Where(d=>d.DeviceId==c.Id && d.SectorId==sectorId).Select(u=>u.Use).FirstOrDefault()
            });       

        var devices = t.ToList();
        return devices;
    }

where repo is a repository of Device

GetAll is the repository method to get the set of Devices .

My questions:

  • Am i doing it the right way?

  • If Yes, then how do I group the data to get a nested collection of

Families
->Categories
--->Devices
DistributionOne
DistributionTwo
..etc

  • If not, then what do I need to correct (my table design?, query?)

Use the GroupBy operator:

var t = repo.GetAll().AsQueryable()
.GroupBy(c => c.Category.Family.ID)
.Select(g => new {
    FamilyID = g.Key,
    DevicesByCategory = g.GroupBy(c => c.Category.ID)
        .Select(g2 => new {
            CategoryID = g2.Key,
            Devices = g2.Select(c => new UserConfigs {
                ....
        })
   })
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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