【问题标题】:Merging multiple List<CustomType> when grouping in LINQ在 LINQ 中分组时合并多个 List<CustomType>
【发布时间】:2020-01-11 22:52:57
【问题描述】:

我有这个模型:

public class AudienceInfo
{
    public int Id { get; set; }
    public string Departments { get; set; }
    public List<CountOfDestination> CountOfDestinations { get; set; }
}

public sealed class CountOfDestination
{
    public string DestinationName { get; set; }
    public int? CountRoom { get; set; }
    public int? CountOfFiles { get; set; }
}

还有 DB 中的这张表。

public class AudienceInfo : IModelWithId
{
    public int Id { get; set; }

    ....

    public RoomPurpose RoomPurpose { get; set; }

    public List<AudienceInfo_File> Files { get; set; }
}

选择“部门”(条件)后,我得到一个数据列表。然后,我 GroupBy “RoomPurpose” 并得到 每行的“CountRoom” = DestinationName。这部分工作正常。 另外,我需要获取 CountOfFiles ...不知道该怎么做

   return dbAudInfo
               .Where(x => x.RightOfPreferentialUse.Id == Id)
               .Select(x => new AudienceInfo
               {
                   Departments = x.RightOfPreferentialUse.Name,
                   Date = date1,
                   CountOfDestinations = dbAudInfo
                        .GroupBy(z => new { z.RoomPurpose })
                        .Select(y => new CountOfDestination
                        {
                            DestinationName = y.Key.RoomPurpose.Name,
                            CountRoom = y.Count(z => z.RightOfPreferentialUse.Id == Id),
                            CountOfFiles = ?????????????????????????
                        }).ToList()
               }).FirstOrDefaultAsync();

如何将 LINQ 查询中的列表与 GroupBy 连接起来。

【问题讨论】:

    标签: asp.net linq


    【解决方案1】:

    另外,我需要获取 CountOfFiles ...不知道该怎么做

    用途:

    CountOfFiles = y.ToList().First(a => a.Id == y.Id).Files.Count()
    

    如何将 LINQ 查询中的列表与 GroupBy 连接起来。

    你能详细说明吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-17
      • 1970-01-01
      • 2013-01-26
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      相关资源
      最近更新 更多