使用LINQ再添加一层列表

时间:2019-01-03 22:38:07

标签: c# linq

我有以下工作的LINQ代码(在.Net 4.5中),它们将扁平化的数据转换为域对象模型,然后转换为相应的JSON。

JSON

{
    "testTIN": "123",
    "measurements": [{
        "measureId": "001",
        "value": {
            "IsEndToEndReported": true,
            "PerformanceMet": 5
        }
    }]
}

LINQ

List<FlattenedRawData> rowList_NoStrata = HelperMethodClasses.GetFlattenedRawData_NoStrata();
List<MeasurementSet__NoStrata> result_NoStrata = rowList_NoStrata.GroupBy(records => records.EffectiveTIN)
                         .Select(y => new MeasurementSet__NoStrata
                         {
                             testTIN = y.Key,
                             measurements = y.Select(i =>
                                     new Measurement_NoStrata()
                                     {
                                         measureId = i.MeasureID,
                                         value = new QualityMeasureValue_NoStrata
                                         {
                                             IsEndToEndReported = true,
                                             PerformanceMet = i.PerformanceMetCount
                                         }
                                     })
                                .ToList()
                         })
                    .ToList();

string requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(result_NoStrata[0]);

现在,我需要在List<MeasurementSet__MultiStrata>而不是List<MeasurementSet__NoStrata>中获得结果,该结果类似但在QualityMeasureValue内部有一个更高级别的对象列表(即List<Strata>)。

{
    "testTIN": "123",
    "measurements": [{
        "measureId": "001",
        "value": {
            "strata": [{
                    "IsEndToEndReported": true,
                    "PerformanceMet": 5,
                    "Stratum": "Level1"
                },
                {
                    "IsEndToEndReported": true,
                    "PerformanceMet": 6,
                    "Stratum": "Level2"
                }
            ]
        }
    }]
}

如何使用LINQ获得List<MeasurementSet__MultiStrata>?一个MeasureId的所有starta元素都应归入同一值元素下。

域类

public class FlattenedRawData
{
    public string EffectiveTIN { get; set; }
    public string MeasureID { get; set; }
    public int PerformanceMetCount { get; set; }
    public string Stratum { get; set; }
}

public class Measurement_NoStrata
{
    public string measureId { get; set; }
    public QualityMeasureValue_NoStrata value { get; set; }
}

public class Measurement_MultiStrata
{
    public string measureId { get; set; }
    public QualityMeasureValue_MultiStrata value { get; set; }
}


public class QualityMeasureValue_NoStrata
{
    public bool IsEndToEndReported { get; set; }
    public int PerformanceMet { get; set; }
}


public class QualityMeasureValue_MultiStrata
{
    public List<Strata> strata = new List<Strata>();
}

public class Strata
{
    public bool IsEndToEndReported { get; set; }
    public int PerformanceMet { get; set; }
}


public class MeasurementSet__NoStrata
{
    public string testTIN { get; set; }
    public List<Measurement_NoStrata> measurements { get; set; }
}

public class MeasurementSet__MultiStrata
{
    public string category { get; set; }
    public string testTIN { get; set; }
    public List<Measurement_MultiStrata> measurements { get; set; }
}

1 个答案:

答案 0 :(得分:1)

这里是如何生成resul_MultiStrata

+x

输出

在这里输出

chmod

完整代码

List<MeasurementSet__MultiStrata> resul_MultiStrata = 
flattenedRawData.GroupBy(groupBy1 => groupBy1.EffectiveTIN)
.Select(level1 => new MeasurementSet__MultiStrata
{
    testTIN = level1.Key,
    measurements = level1.GroupBy(groupBy2 => groupBy2.MeasureID).Select(level2 =>
    new Measurement_MultiStrata()
    {
        measureId = level2.Key,
        value = new QualityMeasureValue_MultiStrata()
        {
        strata = level2.Select(level3 => new Strata
        {
            IsEndToEndReported = true,
            PerformanceMet = level3.PerformanceMetCount
        }).ToList(),
        }
    }).ToList()
}).ToList();