Cav*_*len 9 c# linq join linq-to-sql
如何在没有完全匹配的情况下进行LINQ to SQL连接?例如,假设我有一个form包含数据的表,John Smith (2)我想将其加入Smith表中的字段name.像这样的东西
var query =
from f in db.form
join n in db.name
on f.nameField like '%' + n.firstName + '%'
Run Code Online (Sandbox Code Playgroud)
虽然like关键字似乎不适合我.
p.s*_*w.g 11
你不能like在Linq加入中使用.事实上,你不能使用likeLinq中可言,只有传统的串的方法,如StartsWith,EndsWith或Contains.
你必须做这样的事情:
var query =
from f in db.form
from n in db.name.Where(x => f.nameField.Contains(x.firstName))
...
Run Code Online (Sandbox Code Playgroud)
实际上,有一种方法可以做到这一点,但它并不像使用标准的linq那样整洁:
from c in dc.Organization
where SqlMethods.Like(c.Hierarchy, "%/12/%")
select *;
Run Code Online (Sandbox Code Playgroud)
(借用用户LP在链接问题中的答案)