在LINQ中使用包含到SQL连接 [英] Use contains in LINQ to SQL join

查看:95
本文介绍了在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,只能使用常规字符串方法,例如StartsWithEndsWithContains.

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连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆