使用LINQ填充子模型

时间:2017-07-10 11:16:43

标签: c# linq

我有一个像这样的对象列表

[Code Type][condition Type][Question Type][Description]
[A1][C1][q1][dC1]
[A1][C1][q2][dC1]
[A1][C1][q3][dC1]
[B1][C2][q4][dC2]
[B1][C2][q5][dC2]
[B1][C2][q6][dC2]
[B1][C3][q7][dC3]
[B1][C3][q8][dC3]
[B1][C3][q9][dc3]

我想用一个具有子类的类来映射它,它的子类也有一个子类。 结构是这样的

public Class TypeModel
{
    public string Type{get;set;}
    public List<ConditionModel> Conditions {get;set;}
}

public Class ConditionModel
{
    public string Type{get;set;}
    public string Description {get;set;}
    public List<QuestionModel> Questions {get;set;}
}

public Class QuestionModel
{
    public string Type {get;set;}
}

到目前为止,我已编写此LINQ查询来填充主类型类,但它无效。我需要帮助来创建此查询。

var results = allTypes.GroupBy(type => type.CodeType)
                .Select(grp => new TypeModel
                {
                    Type = grp.Select(i => i.CodeType).First(),
                    Conditions = new List<ConditionModel>
                    {
                        grp.GroupBy(condition => condition.ConditionType)
                        .Select(conditionGrp => new ConditionModel {
                              Type = conditionGrp.Select(i => i.ConditionType).First(),
                              Description = conditionGrp.Select(i => i.Description).First(),
                              Questions = new List<QuestionModel>
                              {
                                  conditionGrp.GroupBy(question => question.QuestionType)
                                  .Select(questionGrp => new QuestionModel
                                  {
                                      Type = questionGrp.Select(i => i.QuestionType).First(),
                                  })
                              }
                        })
                    }
                });

我想用这个查询实现什么?获取TypeModel列表。 如果您注意到该表前三行将获取一个typeModel而另外6行将获取另一个typeModel但它将具有两个ConditonModel并且每个条件Model,3 questionModel。

1 个答案:

答案 0 :(得分:1)

以下内容应该有效:

首先按CodeType分组,然后在每个组中按ConditionType, Description分组,并选择合适的结果。

var results = allTypes.GroupBy(
    type => type.CodeType, // CodeType key selector
    (codeType, elements) => new TypeModel
    {
        Type = codeType,
        Conditions = elements.GroupBy(
            x => new { x.ConditionType, x.Description }, // ConditionType key selector
            x => x.QuestionType, // QuestionType selector as elements of the ConditionType group
            (condition, elements2) => new ConditionModel
            {
                Type = condition.ConditionType,
                Description = condition.Description,
                // Questions transformation
                Questions = elements2.Select(q => new QuestionModel { Type = q }).ToList()
            }).ToList()
    });

如果您对这么多嵌套LINQ感到困惑,使用一些普通的旧循环来创建结果数据没有错:

var results = new List<TypeModel>();
foreach (var item in allTypes.GroupBy(type => type.CodeType))
{
    var conditionsList = new List<ConditionModel>();

    foreach (var item2 in item.GroupBy(x => new { x.ConditionType, x.Description }))
    {
        conditionsList.Add(new ConditionModel
        {
            Type = item2.Key.ConditionType,
            Description = item2.Key.Description,
            Questions = item2.Select(x => new QuestionModel { Type = x.QuestionType }).ToList()
        });
    }

    results.Add(new TypeModel
    {
        Type = item.Key,
        Conditions = conditionsList
    });
}