linq select distinct then order by a different pro

2019-09-02 07:10发布

问题:

I have a table which contains many properties but I am focused on these. Each question has a set of answers and I need to graph each answer. The first step is to select which question then I return a list of answers. I need to select each distinct question and order it by the QOrder.

 <table>
      <tr>
        <td>Question</td>
        <td>ProjectId</td>
        <td>QOrder</td>
      </tr>
      <tr>
        <td>Q10. What is your favourite color?</td>
        <td>10</td>
        <td>1</td>
      </tr>
         <tr>
        <td>Q10. What is your favourite color?</td>
        <td>10</td>
        <td>2</td>
      </tr>
         <tr>
        <td>Q10. What is your favourite color?</td>
        <td>10</td>
        <td>3</td>
      </tr>
       <tr>
        <td>Q31. What is your favourite song?</td>
        <td>10</td>
        <td>4</td>
      </tr>
         <tr>
        <td>Q31. What is your favourite song?</td>
        <td>10</td>
        <td>5</td>
      </tr>
         <tr>
        <td>Q31. What is your favourite song?</td>
        <td>10</td>
        <td>6</td>
      </tr>    
</table>

using (var context = new ApplicationDbContext())
{
    var x = context.Tables
        .Where(t => t.ProjectId == projectId)
        .Select(t => new TableViewModel()
        {
            Question = t.Question,
            ProjectId = t.ProjectId,
            QOrder = t.QOrder
        })
        .Distinct()
        .OrderBy(t => t.QOrder)
        .ToList();
    return x;
}

The distinct doesn't work because the QOrder is not distinct. If I remove QOrder from my viewmodel I won't be able to order by the QOrder.

Is this possible?

SOLUTION:

    using (var context = new ApplicationDbContext())
    {
        return context.Tables
            .Where(t => t.ProjectId == projectId)
            .Select(t => new TableViewModel()
            {
                Question = t.Question,
                ProjectId = t.ProjectId,
                QOrder = t.QOrder
            })
            .ToArray()
            .Distinct(new TableViewModelComparer())
            .OrderBy(t => t.QOrder)
            .ToList();
    }
public class TableViewModelComparer : IEqualityComparer<TableViewModel>
{
    public bool Equals(TableViewModel x, TableViewModel y)
    {
        return x.Question == y.Question;
    }

    public int GetHashCode(TableViewModel obj)
    {
        if (object.ReferenceEquals(obj, null)) return 0;
        return obj.Question == null ? 0 : obj.Question.GetHashCode();
    }
}

回答1:

I think you are not using the Distinct function correctly. I think you want to use the overload of the funtion with the comparerenter link description here

       public class TableViewModelComparer : IEqualityComparer<TableViewModel>
       {
           public bool Equals(TableViewModel x, TableViewModel y)
           {
               return x.QOrder == y.QOrder;
           }

           public int GetHashCode(TableViewModelobj)
           {
               return obj.QOrder.GetHashCode();
           }
       }

and then pass it to the distinct function

   var x = context.Tables
    .Where(t => t.ProjectId == projectId)
    .Select(t => new TableViewModel()
    {
        Question = t.Question,
        ProjectId = t.ProjectId,
        QOrder = t.QOrder
    })        
    .Distinct(comparer: new TableViewModelComparer ())
    .OrderBy(t => t.QOrder)
    .ToList();
return x;