按子句分组与Distinct() [英] Group by clause vs. Distinct()
问题描述
我的ASP.NET自定义列表控件从数据库View
获取其值.检索数据的方法最终返回类型为List<Triplet>
的对象作为控件的数据源.
My ASP.NET custom list control gets its values from a database View
. The method which retrieves data finally returns an object of type List<Triplet>
as a DataSource for the control.
我找到了该方法的三种可能的实现,它们似乎都可以正常工作并给出相同的结果.现在我不确定应该选择其中的哪个.
I figured out three possible implementations for that method which all seem to work fine and give the same results. Now I'm not sure which of them should be preferred.
重点是,我需要按字母顺序从查询中获得唯一的字符串,并且数据库中有很多个重复项.因此,我可以全部获取它们,然后执行Distinct()以获取唯一值...
The point is, that I need unique strings from the query in alphabetical order, and there are many duplicates in the db. So I could fetch them all and then perfom a Distinct() to get unique values...
public override object GetData()
{
return
(
from name in
(
from job in DBConnection.NewDataContext.vJobs
where job.name != null
select job.name
).Distinct().OrderBy(s => s)
select new Triplet(name, name, ListType)
).ToList();
}
...或者我可以使用group by子句并仅选择键:
...or I could use a group by clause and only select the keys:
public override object GetData()
{
return
(
from job in DBConnection.NewDataContext.vJobs
where job.name != null
group job by job.name into names
orderby names.Key
select new Triplet(names.Key, names.Key, ListType)
).ToList();
}
我还想出了以下内容,它为Triplets使用了特殊的EqualityComparer
.实际上,这是我的第一种方法,但是我并不真的喜欢它:
I also came up with the following, which uses a special EqualityComparer
for the Triplets. Actually it was my first approach, but I didn't really like it:
public override object GetData()
{
return
(
from job in DBConnection.NewDataContext.vJobs
where job.name != null
select new Triplet(job.name, job.name, ListType)
).ToList().Distinct(new TripletComparer()).OrderBy(t => (string)t.First).ToList();
}
我认为按解决方案进行的工作大部分工作都留给了数据库(MS SQL Server),这可能是优点还是缺点..我真的不知道.也许Distict()解决方案不得不将过多不必要的数据从db推送到我的方法中?
I think the goup-by-solution leaves most of the work to the database (MS SQL Server), which might be an advantage or disadvantage.. I don't really know. Maybe the Distict()-solutution suffers from having to push too much unneccessary data from the db to my method?
有哪些想法应该实施?似乎因为树木过多我看不到森林...
Any ideas which one should be implemented? It seems I just can't see the forest because of too many trees...
推荐答案
Until there is a need to concern yourself with performance (that is, do not micro-optimize) then you should probably opt for the most readable solution which is clearly to call Distinct
as that conveys your intent very clearly.
如果您真正关心性能,那么我建议您使用 profiler .
If you are truly concerned about the performance, then I suggest you perform some concrete benchmarks using a profiler.
这篇关于按子句分组与Distinct()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!