C# Linq Contains 语句

发布于 2024-10-26 11:42:14 字数 810 浏览 3 评论 0原文

我写了我自己的对象标签,如果找到.Value,我想包含(我想像SQL中那样模拟WHERE IN)

public static List<Question> GetQuestionsIdsWithTags(List<Tag> tags)
        {
    IEnumerable<Question> res = from t in dataClasses.tags
                                    join
                                    qt in dataClasses.question_to_tags on t.id equals qt.tag_id
                                    join q in dataClasses.questions on qt.question_id equals q.id
                                    where tags.Contains<Tag>(new Tag(t.name))
                                    select new Question(q.text) { };

问题是,如果Contains在查询中,我得到

The member 'Core.Literal.Value' has no supported translation to SQL.

Where Literal is the base of Tag 。

我能做些什么?

I wrote my own object Tag and I would like to to contains if the .Value is found (I want to simulate WHERE IN like in SQL)

public static List<Question> GetQuestionsIdsWithTags(List<Tag> tags)
        {
    IEnumerable<Question> res = from t in dataClasses.tags
                                    join
                                    qt in dataClasses.question_to_tags on t.id equals qt.tag_id
                                    join q in dataClasses.questions on qt.question_id equals q.id
                                    where tags.Contains<Tag>(new Tag(t.name))
                                    select new Question(q.text) { };

problem is, if the Contains is in the query, I get

The member 'Core.Literal.Value' has no supported translation to SQL.

Where Literal is the base of Tag.

What can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

一身仙ぐ女味 2024-11-02 11:42:14

您正在尝试执行 new Tag(t.name),但这无法转换为 SQL(数据库服务器无法创建 Tag 类的新实例)。也许这会起作用:

IEnumerable<Question> res = from t in dataClasses.tags
                            join
                            qt in dataClasses.question_to_tags on t.id equals qt.tag_id
                            join q in dataClasses.questions on qt.question_id equals q.id
                            where tags.Select(x => x.name).Contains(t.name)
                            select new Question(q.text) { };

You're trying to do new Tag(t.name), but this cannot be translated into SQL (the database server can't create new instances of your Tag class). Perhaps this would work:

IEnumerable<Question> res = from t in dataClasses.tags
                            join
                            qt in dataClasses.question_to_tags on t.id equals qt.tag_id
                            join q in dataClasses.questions on qt.question_id equals q.id
                            where tags.Select(x => x.name).Contains(t.name)
                            select new Question(q.text) { };
白色秋天 2024-11-02 11:42:14

如果 tagsList,您应该会发现:

where tags.Contains(t.Name)

工作正常;但它能理解的内容是有限的(更重要的是,以 TSQL 形式编写)。

if tags is a List<string>, you should find that:

where tags.Contains(t.Name)

works fine; but there are limits to what it can understand (and more importantly, write as TSQL).

赠我空喜 2024-11-02 11:42:14

仅当 LinQ Contains() 语句由包含 intstring 等基本数据类型的 List 执行时,该语句才能转换为 SQL。如果您需要将 tags 列表转换为字符串或整数列表,那么它应该可以工作。

The LinQ Contains() statement can only be translated to SQL if it is performed by a List containing basic datatypes like int or string. If you need to cast your tags list to a List of strings or ints, then it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文