本文介绍了在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连接
发布评论