1

次の LINQ 式があります。

public IList<Objective> GetObjectives(int examId)
    {
        var objectives = _objectivesRepository
            .GetAll()
            .Where(o => o.ExamId == examId || examId == 0)
            .Include(o => o.ObjectiveDetails)
            .ToList();
        return objectives;
    }

public partial class Objective
{
    public int ObjectiveId { get; set; }
    public int ExamId { get; set; }
    public int Number { get; set; }
    public virtual ICollection<ObjectiveDetail> ObjectiveDetails { get; set; }
}

public partial class ObjectiveDetail
{
    public int ObjectiveDetailId { get; set; }
    public int ObjectiveId { get; set; }
    public int Number { get; set; }
    public string Text { get; set; }
}

NumberObjective が の順に返され、ObjectiveDetails が の順に返されるように、この式を変更するにはどうすればよいNumberですか?

4

3 に答える 3

2

ここで投影を使用できます。

var objectives = _objectivesRepository
        .GetAll()
        .Where(o => o.ExamId == examId || examId == 0)
        .Include(o => o.ObjectiveDetails)
        .Select(x => new Objective {
            ObjectiveId = x.Id,
            ExamId = x.ExamId,
            Number = x.Number,
            ObjectiveDetails = x.ObjectiveDetails.OrderBy(d => d.Number).ToList()
        })
        .OrderBy(x => x.Number)
        .ToList();
return objectives;
于 2013-11-04T11:10:28.933 に答える
2
public IList<Objective> GetObjectives(int examId)
    {
        var objectives = _objectivesRepository
            .GetAll()
            .Where(o => o.ExamId == examId || examId == 0)
            .Include(o => o.ObjectiveDetails.OrderBy(d => d.Number))
            .OrderBy(o => o.Number);
            .ToList();
        return objectives;
    }
于 2013-11-04T11:10:57.183 に答える
1

残念ながら、ネストされたオブジェクトの順序付けは、メイン クエリとは別に行う必要があります。Includeステートメントに要素を順番に取得させる方法があるとは思いません。

public IList<Objective> GetObjectives(int examId)
{
    var objectives = _objectivesRepository
        .GetAll()
        .Where(o => o.ExamId == examId || examId == 0)
        .Include(o => o.ObjectiveDetails)
        .ToList();
    return objectives.OrderBy(o => o.Number)
        .Select(o => {
            var record = o;
            record.ObjectiveDetails = record.ObjectiveDetails.OrderBy(d => d.Number).ToList();
            return record;
        });
}
于 2013-11-04T11:28:19.477 に答える