选择不同的Linq

时间:2012-11-21 18:28:42

标签: c# .net linq linq-to-dataset

我在数据库中有问答表。我使用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; }
    }      
}

2 个答案:

答案 0 :(得分:2)

我相信你只想用.GroupBy(n =&gt; 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 =&gt; n.Oid):

GroupBy(n=>n.Oid).Select(g => g.First()).ToList();

答案 1 :(得分: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();