我有這樣的一個表:問題與DISTINCT和LINQ2SQL
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
,我要儘量讓不同行(DISTINCT 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 (interpretation interp in ViewBag.Interps)
System.NotSupportedException: Explicit construction of entity type 'vslovare.Models.interpretation' in query is not allowed.
,這真是個方式,採取不同的列,並在完成行這樣的有:
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
在第一個代碼段,爲什麼之前含混做一個ToList? ToList將導致所有未被Where從數據庫中拉下來的記錄。 – rsbarro 2011-03-20 15:35:36
因爲。凡(I => i.iddictionary == iddict).Distinct(新WordsInDictionaryDistinct())得到一個更錯誤:System.NotSupportedException:用於查詢運算符 '獨特' 不支持的過載。 – FSou1 2011-03-20 15:45:29
我認爲這是因爲對Distinct的調用中有新的WordsInDictionaryDistinct()。這應該是一個IEqualityComparer。你可以將WordsInDictionaryDistinct的實現添加到問題中嗎? – rsbarro 2011-03-20 15:52:48