如何将sql查询转换为linq查询 [英] How to convert sql query to linq query
本文介绍了如何将sql查询转换为linq查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi ,
ALTER PROCEDURE [dbo].[sp_GetAllEmployeeByUserId] @UserId INT
AS
BEGIN
SET NOCOUNT ON;
SELECT u.UserID
,u.Code as UserCode
,isnull(e.EmployeeID, 0) AS EmployeeId
,e.Code as EmployeeCode
,e.FirstName
,e.MiddleName
,e.LastName
,e.[Address]
,e.Latitude
,e.Longitude
FROM [User] AS u WITH (NOLOCK)
LEFT JOIN Employee AS e WITH (NOLOCK) ON u.EmployeeID = e.EmployeeID
where u.UserID=@UserId
END
GO
这是我的sp,
请告诉我如何在linq中写入相同的文件
This is my sp ,
please tell me how to write same in linq to sql
推荐答案
你好,
在LINQ TO SQL中实现NOLOCK
请参阅: 使用LINQ to SQL和LINQ to Entities实现NOLOCK
现在,对于左外连接必须遵循:
a。 执行左外连接
b 。 内连接,交叉连接和左外连接LINQ to SQL
现在尝试这种方式
Hello ,
To ImplementNOLOCK
in LINQ TO SQL Refer : Implementing NOLOCK with LINQ to SQL and LINQ to Entities
Now , for left outer join must follow :
a. Perform Left Outer Joins
b. Inner Join, Cross Join and Left Outer Join With LINQ to SQL
Now try this way
using (var txn = new TransactionScope(
TransactionScopeOption.Required,
new TransactionOptions
{
IsolationLevel = IsolationLevel.ReadUncommitted
}
))
{
// Your LINQ to SQL query goes here
var query=from u in User join
e in Employee on u.EmployeeID equals e.EmployeeID into result
from v in result.DefaultIfEmpty()
select new
{
UserID =u.UserID
,UserCode =u.Code
,EmployeeId= e.EmployeeID ==null ? 0 : e.EmployeeID
,EmployeeCode=e.Code
,FirstName=e.FirstName
,MiddleName=e.MiddleName
,LastName=e.LastName
,Address=e.Address
,Latitude=e.Latitude
,Longitude=e.Longitude
};
}
谢谢
Thanks
这篇关于如何将sql查询转换为linq查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文