C# 复杂查询linqlambda
我试图在单个查询中提取数据。 因为它涉及很多表,所以我有点困在分组部分 我没有足够的声誉来发布我的桌子设计的图片。 所以,我给PK,FKC# 复杂查询linqlambda,c#,linq,entity-framework-4.1,C#,Linq,Entity Framework 4.1,我试图在单个查询中提取数据。 因为它涉及很多表,所以我有点困在分组部分 我没有足够的声誉来发布我的桌子设计的图片。 所以,我给PK,FK Sector (SectorId) Device (DeviceId:PK, CategoryId:FK) Ratio (SectorId,DeviceId) Use (UseId) DeviceUse (SectorId,DeviceId,UseId) Family (FamilyId) Category (CategoryId) Level
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)
我努力实现的目标是:
给定一个扇区ID
,从给定的所有表中检索所有相关信息
结果将是:
所有设备
按族分组
按类别分组
以及所有比率
(对于给定的扇区ID
和设备ID
)
所有设备使用
(用于相关扇区ID
和设备ID
)和相关设备使用
用于设备ID
以及所有Consu
(用于相关deviceId
,levelId
,ageId
)和相关Age
和Level
以及所有分发单
(用于相关设备ID
,级别ID
,部门ID
)和相关级别
以及所有分布两个(适用于相关设备ID
,级别ID
,部门ID
,年龄ID
)和相关级别
到目前为止,我得到了如下方法
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;
}
public IEnumerable GetDeviceType(int sectorId)
{
var t=repo.GetAll().AsQueryable()
.选择(
c=>newuserconfig
{
设备=新设备{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(),
类别=c.类别,
族=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)。选择(u=>u.Use)。FirstOrDefault()
});
var设备=t.ToList();
返回装置;
}
其中repo
是设备的存储库
GetAll
是获取设备集的存储库方法
我的问题是:
- 我这样做对吗
- 如果是,那么如何对数据进行分组以获得数据的嵌套集合
家庭
->类别
--->设备
分配单
分配2
等等
- 如果没有,那么我需要更正什么(我的表设计?、查询?)
使用GroupBy
操作员:
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 {
....
})
})
});
这里真的有问题吗,或者你只是想对你的代码提出批评?如果是这样的话,这个问题就离题了,你应该在上发帖。我想知道如何进行分组以获得嵌套集合分解它以使用一些方法。谢谢,这就是我正在做的,投影到viewmodels并使用分组