DISTINCT 和 Linq2SQL 的问题

发布于 2024-10-25 00:41:25 字数 2362 浏览 1 评论 0原文

我有一个像这样的表:

idinterpretation | iddictionary | idword | meaning
1                   1              1115     hello
2                   1              1115     hi
3                   1              1115     hi, bro
5                   1              1118     good bye
6                   1              1118     bye-bye
7                   2              1119     yes 
8                   2              1119     yeah
9                   2              1119     all rigth

我尝试获取不同的行(不同的idword)。所以,一开始我尝试过:

return dc.interpretations.Where(i => i.iddictionary == iddict).
    ToList<interpretation>().Distinct(new WordsInDictionaryDistinct()).
    OrderBy(w => w.word.word1).Skip(iSkip).Take(iTake);

但是我的表中有大约 300.000 行,这是错误的解决方案。

然后,我尝试:

    IEnumerable<interpretation> res = (from interp in dc.interpretations
                                      group interp by interp.idword into groupedres
                                      select new interpretation
                                      {
                                          idword = groupedres.Key,
                                          idinterpretation = groupedres.SingleOrDefault(i => i.idword == groupedres.Key).idinterpretation,
                                          interpretation1 = groupedres.SingleOrDefault(i => i.idword == groupedres.Key).interpretation1,
                                          iddictionary = groupedres.SingleOrDefault(i => i.idword == groupedres.Key).iddictionary
                                      }).Skip(iSkip).Take(iTake);

并且出现错误:@foreach(ViewBag.Interps 中的解释插值) System.NotSupportedException:查询中实体类型“vslovare.Models.interpretation”的显式构造不是

这真的是一种获取不同行并完成这样的行的方法吗:

idinterpretation | iddictionary | idword | meaning
1                   1              1115     hello
5                   1              1118     good bye
7                   2              1119     yes 


字典:

dictionary table

iddictionary | dictionary_name

单词:

word table

idword | word_name

解释:

interpretation table

idinterpretation | iddictionary | idword | meaning

I have a table like this:

idinterpretation | iddictionary | idword | meaning
1                   1              1115     hello
2                   1              1115     hi
3                   1              1115     hi, bro
5                   1              1118     good bye
6                   1              1118     bye-bye
7                   2              1119     yes 
8                   2              1119     yeah
9                   2              1119     all rigth

And i try to get distinct rows (DISTINCT idword). So, at first i tried:

return dc.interpretations.Where(i => i.iddictionary == iddict).
    ToList<interpretation>().Distinct(new WordsInDictionaryDistinct()).
    OrderBy(w => w.word.word1).Skip(iSkip).Take(iTake);

But i have about 300.000 rows in my table and it is wrong solution.

Then, i tried:

    IEnumerable<interpretation> res = (from interp in dc.interpretations
                                      group interp by interp.idword into groupedres
                                      select new interpretation
                                      {
                                          idword = groupedres.Key,
                                          idinterpretation = groupedres.SingleOrDefault(i => i.idword == groupedres.Key).idinterpretation,
                                          interpretation1 = groupedres.SingleOrDefault(i => i.idword == groupedres.Key).interpretation1,
                                          iddictionary = groupedres.SingleOrDefault(i => i.idword == groupedres.Key).iddictionary
                                      }).Skip(iSkip).Take(iTake);

and i took error: @foreach (interpretation interp in ViewBag.Interps) System.NotSupportedException: Explicit construction of entity type 'vslovare.Models.interpretation' in query is not allowed.

Is it really a way to take distinct rows and to have in finish rows like this:

idinterpretation | iddictionary | idword | meaning
1                   1              1115     hello
5                   1              1118     good bye
7                   2              1119     yes 

?


dictionaries:

dictionary table

iddictionary | dictionary_name

words:

word table

idword | word_name

interpretations:

interpretation table

idinterpretation | iddictionary | idword | meaning

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮光沉寂 2024-11-01 00:41:26

我认为您的第二次尝试即将完成 - 您可能需要使用 GroupBy 子句才能在 SQL 中实现此操作。

比如:

var query = from row in dc.interpretations
            where row.iddictionary == iddict
            group row by idword into grouped
            select grouped.FirstOrDefault();

return query.OrderBy(w => w.word.word1).Skip(iSkip).Take(iTake);

关于为什么你的查询花费太长的时间 - 一般来说,如果你的查询很慢,那是因为你正在搜索和返回的数据非常大 - 或者因为它在数据库级别的索引很差。为了帮助找出答案,它正在分析或分析您的查询 - 请参阅 MSDN 上的这篇文章 http ://msdn.microsoft.com/en-us/magazine/cc163749.aspx

I think your second attempt is almost there - you probably need to use a GroupBy clause to get this working within SQL.

Something like:

var query = from row in dc.interpretations
            where row.iddictionary == iddict
            group row by idword into grouped
            select grouped.FirstOrDefault();

return query.OrderBy(w => w.word.word1).Skip(iSkip).Take(iTake);

On why your query is taking too long - in general, if your query is slow it will be because the data you are searching and returning is really large - or because it is poorly indexed at the database level. To help find out, it is analysing or profiling your query - see this article on MSDN http://msdn.microsoft.com/en-us/magazine/cc163749.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文