且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将sql转换为linq

更新时间:2023-02-14 10:21:28

丑陋的一个:



即使在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 }
;