powered by simpleCommunicator - 2.0.59     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / ADO.NET, LINQ, Entity Framework, NHibernate, DAL, ORM [игнор отключен] [закрыт для гостей] / Linq2SQL Distinct
2 сообщений из 2, страница 1 из 1
Linq2SQL Distinct
    #37174405
FSou1
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Добрый день.

Есть 3 таблицы:

Код: plaintext
1.
2.
dictionary table (50 записей)

iddictionary | dictionary_name

Код: plaintext
1.
2.
word table (130000 записей)

idword | word_name

Код: plaintext
1.
2.
interpretation table (1 000 000 записей)

idinterpretation | iddictionary | idword | meaning

Как можно получить уникальные записи по полю idword из таблицы interpretation, без использования ToList?

1.
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
return dc.interpretations.Where(i => i.iddictionary == iddict).Distinct(new WordsInDictionaryDistinct()).OrderBy(w => w.word.word1).Skip(iSkip).Take(iTake);

    public class WordsInDictionaryDistinct : IEqualityComparer<interpretation>
    {
        public bool Equals(interpretation x, interpretation y)
        {
            return x.idword.Equals(y.idword) && y.idword.Equals(x.idword);
        }

        public int GetHashCode(interpretation obj)
        {
            return obj.idword.GetHashCode();
        }
    }

Ошибка: Unsupported overload used for query operator 'Distinct'

2.
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
return dc.interpretations.Where(i => i.iddictionary == iddict).ToList().Distinct(new WordsInDictionaryDistinct()).OrderBy(w => w.word.word1).Skip(iSkip).Take(iTake);

    public class WordsInDictionaryDistinct : IEqualityComparer<interpretation>
    {
        public bool Equals(interpretation x, interpretation y)
        {
            return x.idword.Equals(y.idword) && y.idword.Equals(x.idword);
        }

        public int GetHashCode(interpretation obj)
        {
            return obj.idword.GetHashCode();
        }
    }

Результат: Работает, однако ToList() на 60000 записей (в одном из словарей) выполняется очень и очень долго.

3.
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
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);

Ошибка: При попытке перебора полученного результата, получаю: Explicit construction of entity type 'vslovare.Models.interpretation' in query is not allowed

4. С анонимными типами тоже не получится.

п.с. Как же всё таки это можно сделать?

Из этого:
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
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

Получить это:

Код: plaintext
1.
2.
3.
idinterpretation | iddictionary | idword | meaning
1                   1              1115     hello
5                   1              1118     good bye
7                   2              1119     yes 
...
Рейтинг: 0 / 0
Linq2SQL Distinct
    #37176109
FSou1
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
FSou1,

Однако вот так.

Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
        public IEnumerable<interpretation> GetWords(int iddict, int iSkip, int iTake)
        {
            var query = from row in dc.interpretations
                        where row.iddictionary == iddict
                        group row by row.idword into grouped
                        select grouped.FirstOrDefault();

            return query.OrderBy(w => w.word.word1).Skip(iSkip).Take(iTake);
        
        }
...
Рейтинг: 0 / 0
2 сообщений из 2, страница 1 из 1
Форумы / ADO.NET, LINQ, Entity Framework, NHibernate, DAL, ORM [игнор отключен] [закрыт для гостей] / Linq2SQL Distinct
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]