linq选择不同,然后按不同的属性排序 [英] linq select distinct then order by a different property
问题描述
< table>
< tr>
< td>问题< / td>
< td> ProjectId< / td>
< td> QOrder< / td>
< / tr>
< tr>
< td> Q10。你最喜欢的颜色是什么?< / td>
< td> 10< / td>
< td> 1< / td>
< / tr>
< tr>
< td> Q10。你最喜欢的颜色是什么?< / td>
< td> 10< / td>
< td> 2< / td>
< / tr>
< tr>
< td> Q10。你最喜欢的颜色是什么?< / td>
< td> 10< / td>
< td> 3< / td>
< / tr>
< tr>
< td> Q31。你最喜欢的歌是什么?< / td>
< td> 10< / td>
< td> 4< / td>
< / tr>
< tr>
< td> Q31。你最喜欢的歌是什么?< / td>
< td> 10< / td>
< td> 5< / td>
< / tr>
< tr>
< td> Q31。你最喜欢的歌是什么?< / td>
< td> 10< / td>
< td> 6< / td>
< / tr>
< / table>
使用(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;
}
由于QOrder不区分,distinct不起作用。
如果我从我的viewmodel中删除QOrder,我将无法由QOrder订购。
这是可能吗?
解决方案:
using(var context = new ApplicationDbContext())
{
return context.Tables
.Where(t => t.ProjectId == projectId)
.Select(t => new TableViewModel()
{
问题= 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();
}
}
认为您没有正确使用Distinct功能。我想你想使用比较器的功能超负荷请在此输入链接描述
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();
}
}
然后将其传递给不同的函数
var x = context.Tables
.Where(t => t.ProjectId == projectId)
。选择(t => new TableViewModel()
{
Question = t.Question,
ProjectId = t.ProjectId,
QOrder = t.QOrder
})
.Distinct(比较器:新的TableViewModelComparer())
.OrderBy(t => t.QOrder)
.ToList();
return x;
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();
}
}
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;
这篇关于linq选择不同,然后按不同的属性排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!