开发者

Problem with DISTINCT and Linq2SQL

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 | mea开发者_开发百科ning
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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜