LINQ 不同计数问题
试图了解出了什么问题,但我真的做不到。所以:
return dc.dictionaries.Select(
d => new AdminDictionaryViewModel
{ dictionary = d,
WordQuantity = dc.interpretations.Where(
i => i.iddictionary == d.iddictionary).Count() });
这工作正常,但如果我更改为:
return dc.dictionaries.Select(
d => new AdminDictionaryViewModel
{ dictionary = d,
WordQuantity = dc.interpretations.Where(
i => i.iddictionary == d.iddictionary)
.Distinct(new WordsInDictionaryDistinct()).Count() });
我的班级是:
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();
}
}
我有:
dictionary table
iddictionary | dictionary_name
word table
idword | word_name
interpretation table
idinterpretation | iddictionary | idword
我需要在每个字典中只有不同的 idword,bcz 如果我在第一个查询中返回,可能会有大约 10 个带有 idword 的单词= 15,但这是一个单词,而不是 15:
解释表
idinterpretation | iddictionary | idword | meaning
1 1 1115 hello
2 1 1115 hi
3 1 1115 hi, bro
4 1 1116
5 1 1118 good bye
6 1 1118 bye-bye
7 2
8 2
在我的第二次查询之后,我希望我能得到 {d = 1, WordQuantity = 3},但是
查询运算符“Distinct”使用了不支持的重载.
我已经尝试过
.ToList
但仍然出现相同的错误。
真的,不知道该怎么办:(
Trying to understand what's wrong, but i really cant do it. So:
return dc.dictionaries.Select(
d => new AdminDictionaryViewModel
{ dictionary = d,
WordQuantity = dc.interpretations.Where(
i => i.iddictionary == d.iddictionary).Count() });
This works fine, but if i change to:
return dc.dictionaries.Select(
d => new AdminDictionaryViewModel
{ dictionary = d,
WordQuantity = dc.interpretations.Where(
i => i.iddictionary == d.iddictionary)
.Distinct(new WordsInDictionaryDistinct()).Count() });
And my class is:
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();
}
}
I have:
dictionary table
iddictionary | dictionary_name
word table
idword | word_name
interpretation table
idinterpretation | iddictionary | idword
And i need to have just distinct idword in each of dictionary, bcz if i return in my first query, there could be about 10 words with idword = 15, but this is one word, not 15:
interpretation table
idinterpretation | iddictionary | idword | meaning
1 1 1115 hello
2 1 1115 hi
3 1 1115 hi, bro
4 1 1116
5 1 1118 good bye
6 1 1118 bye-bye
7 2
8 2
After my second query, i hope i'll get {d = 1, WordQuantity = 3}, but there is
Unsupported overload used for query operator 'Distinct'.
I've tried
.ToList<interpretation>().Distinct(new WordsInDictionaryDistinct()).Count()
but still the same error.
Really, dont know what to do :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是
IEqualityComparer
无法转换为 SQL。尝试替换
为
The problem is that the
IEqualityComparer
can not be translated to SQL.Try replacing
with
我认为您最好将查询更改为分组而不是现在的方式,您也可能会获得更好的性能。
I think you will be better off changing your query to group instead of the way it is now, you will probably get better performance, too.