0

我在数据库中有问答表。我使用 Join 来选择带有答案的问题,并尝试使用 LINQ 获取问题和答案。这是我的代码:

List<Question> newQuestionList = 
    (from q in dt.AsEnumerable()
     where (q.Field<Guid>("Question") != null))
     select new Question
     {
         Oid = q.Field<Guid>("Question"),
         QuestionContext = q.Field<String>("QuestionContext"),
         Priority = q.Field<Int32>("Priority"),
         Order = q.Field<Int32>("OrderQuestion"),
         Subject = q.Field<Guid>("Subject"),
         Answers = (from a in dt.AsEnumerable()
                    where a.Field<Guid>("Question") == q.Field<Guid>("Question")
                    select
                    new Answer
                    {
                        Oid = a.Field<Guid>("AnswerOid"),
                        AnswerContext = a.Field<String>("Answer"),
                        IsCorrect = a.Field<bool>("Correct")
                    }).ToList()
         }).Distinct().ToList();

有很多行具有相同的问题(Id)。它遍历所有。对于一个问题,我只需要迭代一次。如果我有 7 个问题,并且每个问题都有 4 个答案,那么这段代码会给我一个计数为 28 的列表。它的计数必须是 7。有人可以帮忙吗?

public class Question
{
    private Guid oid;

    public Guid Oid
    {
        get { return oid; }
        set { oid = value; }
    }

    private string questionContext;

    public string QuestionContext
    {
        get { return questionContext; }
        set { questionContext = value; }
    }

    private int priority;

    public int Priority
    {
        get { return priority; }
        set { priority = value; }
    }

    private Guid subject;

    public Guid Subject
    {
        get { return subject; }
        set { subject = value; }
    }

    private List<Answer> answers;

    public List<Answer> Answers
    {
        get { return answers; }
        set { answers = value; }
    }

    private int order;

    public int Order
    {
        get { return order; }
        set { order = value; }
    }
}

public class Answer 
{
    private Guid oid;

    public Guid Oid
    {
        get { return oid; }
        set { oid = value; }
    }

    private string answerContext;

    public string AnswerContext
    {
        get { return answerContext; }
        set { answerContext = value; }
    }

    private bool isCorrect;

    public bool IsCorrect
    {
        get { return isCorrect; }
        set { isCorrect = value; }
    }      
}
4

2 回答 2

2

我相信您只想使用 .GroupBy(n=>n.Oid) 对您的问题进行分组(我假设每个问题都有一个唯一的 Oid,因为它是一个 Guid)

var questions = (from q in dt.AsEnumerable()
                 where (q.Field<Guid>("Question") != null))
                 select new Question
                 {
                     Oid = q.Field<Guid>("Question"),
                     QuestionContext = q.Field<String>("QuestionContext"),
                     Priority = q.Field<Int32>("Priority"),
                     Order = q.Field<Int32>("OrderQuestion"),
                     Subject = q.Field<Guid>("Subject"),
                     Answers = (from a in dt.AsEnumerable()
                                where a.Field<Guid>("Question") == q.Field<Guid>("Question")
                                select new Answer
                                {
                                    Oid = a.Field<Guid>("AnswerOid"),
                                    AnswerContext = a.Field<String>("Answer"),
                                    IsCorrect = a.Field<bool>("Correct")
                                }).ToList()
                 }).GroupBy(n=>n.Oid).ToList();

这不会返回平面集合。如果我正确理解您的结构,这将返回 7 个独特的问题,这些问题已加入到他们的答案中。如果你不关心其他人,你可以选择通过获得第一个答案来展平这个集合。您可以将此附加到您的 GroupBy(n=>n.Oid) 中:

GroupBy(n=>n.Oid).Select(g => g.First()).ToList();
于 2012-11-21T18:45:27.870 回答
2

我认为如果您先进行分组,那么您将不必为每个问题枚举整个表格

List<Question> questions = 
    (from q in dt.AsEnumerable()
     where (q.Field<Guid>("Question") != null)
     group q by new {  Oid = q.Field<Guid>("Question"),
                       QuestionContext = q.Field<String>("QuestionContext"),
                       Priority = q.Field<Int32>("Priority"),
                       Order = q.Field<Int32>("OrderQuestion"),
                       Subject = q.Field<Guid>("Subject")
    } into g
    select new Question {
        Oid = g.Key.Oid,
        QuestionContext = g.Key.QuestionContext,
        Priority = g.Key.Priority,
        Order = g.Key.Order,
        Subject = g.Key.Subject,
        Answers = g.Select(a => new Answer() 
        {
            Oid = a.Field<Guid>("AnswerOid"),
            AnswerContext = a.Field<String>("Answer"),
            IsCorrect = a.Field<bool>("Correct")
        }).ToList()
    }).ToList();  
于 2012-11-21T19:37:58.447 回答