title: Select distinct Linq
body: <p>I have Question and Answer tables in a database. I used Join to select questions with answers and tried to get questions and answers with LINQ. Here is my code:</p>
  
  <pre><code>List&lt;Question&gt; newQuestionList = 
      (from q in dt.AsEnumerable()
       where (q.Field&lt;Guid&gt;("Question") != null))
       select new Question
       {
           Oid = q.Field&lt;Guid&gt;("Question"),
           QuestionContext = q.Field&lt;String&gt;("QuestionContext"),
           Priority = q.Field&lt;Int32&gt;("Priority"),
           Order = q.Field&lt;Int32&gt;("OrderQuestion"),
           Subject = q.Field&lt;Guid&gt;("Subject"),
           Answers = (from a in dt.AsEnumerable()
                      where a.Field&lt;Guid&gt;("Question") == q.Field&lt;Guid&gt;("Question")
                      select
                      new Answer
                      {
                          Oid = a.Field&lt;Guid&gt;("AnswerOid"),
                          AnswerContext = a.Field&lt;String&gt;("Answer"),
                          IsCorrect = a.Field&lt;bool&gt;("Correct")
                      }).ToList()
           }).Distinct().ToList();
  </code></pre>
  
  <p>There are a lot of rows that have the same question (Id). It iterates through all. I need to iterate only once for a question. If I have 7 questions, and every question has 4 answers, this code gives me a list that's count is 28. Its count must be 7. Can anyone help?</p>
  
  <pre><code>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&lt;Answer&gt; answers;
  
      public List&lt;Answer&gt; 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; }
      }      
  }
  </code></pre>