C# Linq Contains 语句
我写了我自己的对象标签,如果找到.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在尝试执行
new Tag(t.name)
,但这无法转换为 SQL(数据库服务器无法创建 Tag 类的新实例)。也许这会起作用: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:如果
tags
是List
,您应该会发现:工作正常;但它能理解的内容是有限的(更重要的是,以 TSQL 形式编写)。
if
tags
is aList<string>
, you should find that:works fine; but there are limits to what it can understand (and more importantly, write as TSQL).
仅当 LinQ
Contains()
语句由包含int
或string 等基本数据类型的
。如果您需要将List
执行时,该语句才能转换为 SQLtags
列表转换为字符串或整数列表,那么它应该可以工作。The LinQ
Contains()
statement can only be translated to SQL if it is performed by aList
containing basic datatypes likeint
orstring
. If you need to cast yourtags
list to a List of strings or ints, then it should work.