LINQ 不同计数问题

发布于 2024-10-22 01:40:04 字数 2056 浏览 2 评论 0原文

试图了解出了什么问题,但我真的做不到。所以:

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().Distinct(new WordsInDictionaryDistinct()).Count()

但仍然出现相同的错误。

真的,不知道该怎么办:(

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日久见人心 2024-10-29 01:40:04

问题是 IEqualityComparer 无法转换为 SQL。

尝试替换

.Distinct(new WordsInDictionaryDistinct())

.Select(x => x.idword).Distinct()

The problem is that the IEqualityComparer can not be translated to SQL.

Try replacing

.Distinct(new WordsInDictionaryDistinct())

with

.Select(x => x.idword).Distinct()
给不了的爱 2024-10-29 01:40:04

我认为您最好将查询更改为分组而不是现在的方式,您也可能会获得更好的性能。

return (from d in dc.dictionaries
        join i in dc.interpretations on d.iddictionary == i.iddictionary
        group i by d into g
        select AdminDictionaryViewModel()
        {
            dictionary = g.Key,
            WorkQuantity = g.Distinct(o => o.idword).Count()
        });

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.

return (from d in dc.dictionaries
        join i in dc.interpretations on d.iddictionary == i.iddictionary
        group i by d into g
        select AdminDictionaryViewModel()
        {
            dictionary = g.Key,
            WorkQuantity = g.Distinct(o => o.idword).Count()
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文