如何将sql转换为linq [英] how to conver sql to linq
本文介绍了如何将sql转换为linq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
选择 cl.clientid,clad.address1,clad.address2 来自客户端cl
join ClientAddress clad on cl.id = clad.clientguid
其中 clad.Address2 喜欢 ' < span class =code-string> 150%' 和 cl.clientid = ' 650450'
解决方案
丑陋的一个:
即使在LINQ中你仍然可以运行SQL查询。示例
IEnumerable< Customer> results = db.ExecuteQuery< Customer>
( @ SELECT c1.custid as CustomerID,c2.custName as ContactName
FROM customer1 as c1,customer2为c2
WHERE c1.custid = c2.custid
);
好一个:
在设计师中模拟你的模式,你不应该遇到正确的问题。
代码会像
var query = 来自 cl 客户端
join clad in ClientAddress on cl.id equals clad.clientguid
where cl.clientid = ' 650450'&& clad.Address2.StartsWith(' 150')
选择 new {l.clientid,clad.address1,clad.address2}
;
select cl.clientid,clad.address1,clad.address2 from Client cl
join ClientAddress clad on cl.id=clad.clientguid
where clad.Address2 like '150%' and cl.clientid='650450'
解决方案
Ugly one:
Even in LINQ you still can run sql queries. Example
IEnumerable<Customer> results = db.ExecuteQuery<Customer> (@"SELECT c1.custid as CustomerID, c2.custName as ContactName FROM customer1 as c1, customer2 as c2 WHERE c1.custid = c2.custid" );
Good one:
Model your schema in designer, and you should not experience problems with righting.
Code will be smth like
var query = from cl in Client join clad in ClientAddress on cl.id equals clad.clientguid where cl.clientid='650450' && clad.Address2.StartsWith('150') select new { l.clientid,clad.address1,clad.address2 } ;
这篇关于如何将sql转换为linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文