在LINQ中使用包含到SQL连接

编程入门 行业动态 更新时间:2024-10-28 06:26:56
本文介绍了在LINQ中使用包含到SQL连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在不完全匹配的情况下进行LINQ to SQL连接?例如,假设我有一个包含数据John Smith (2)的表form,并且我想将其连接到表name中的字段Smith.像这样

How can I do a LINQ to SQL join without an exact match? For example, say I have a table form with the data John Smith (2) and I want to join it to the field Smith in table name. Something like this

var query = from f in db.form join n in db.name on f.nameField like '%' + n.firstName + '%'

尽管like关键字似乎对我来说不可用.

Although the like keyword doesn't seem to be available to me.

推荐答案

您不能在Linq连接中使用like.实际上,您完全不能在Linq中使用like,只能使用常规字符串方法,例如StartsWith,EndsWith或Contains.

You can't use like in a Linq join. In fact, you can't use like in Linq at all, only conventional string methods like StartsWith, EndsWith, or Contains.

您必须执行以下操作:

var query = from f in db.form from n in db.name.Where(x => f.nameField.Contains(x.firstName)) ...

更多推荐

在LINQ中使用包含到SQL连接

本文发布于:2023-10-14 11:30:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1490948.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:LINQ   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!