我有这样的一个表:问题与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