Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 复杂查询linqlambda_C#_Linq_Entity Framework 4.1 - Fatal编程技术网

C# 复杂查询linqlambda

C# 复杂查询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

我试图在单个查询中提取数据。 因为它涉及很多表,所以我有点困在分组部分

我没有足够的声誉来发布我的桌子设计的图片。 所以,我给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)
我努力实现的目标是:

给定一个
扇区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并使用分组