如何将T-SQL转换为Linq C#
如何将此查询转换为Linq或Lambda表达式如何将T-SQL转换为Linq C#,c#,linq,tsql,lambda,C#,Linq,Tsql,Lambda,如何将此查询转换为Linq或Lambda表达式 SELECT SURECTNM.RolId, IZIN_LIST.* FROM ( SELECT DISTINCT Damga, SurecId FROM WEB_KULLANICISUREC ) IZIN_SUREC LEFT JOIN WEB_SURECROL SURECTNM ON IZIN_SUREC.SurecId = SURECTNM.SurecID LEFT JOIN WEB_KULLANICISURE
SELECT SURECTNM.RolId,
IZIN_LIST.*
FROM (
SELECT DISTINCT Damga, SurecId FROM WEB_KULLANICISUREC
) IZIN_SUREC
LEFT JOIN WEB_SURECROL SURECTNM
ON IZIN_SUREC.SurecId = SURECTNM.SurecID
LEFT JOIN WEB_KULLANICISUREC AS IZIN_LIST
ON IZIN_SUREC.Damga = IZIN_LIST.Damga and SURECTNM.RolId = IZIN_LIST.RolId
WHERE IZIN_SUREC.Damga = '9EE15E44-C7B3-4AD5-A73F-3A2B4B49C0CC'
我先用数据库
WEB_Kullaniscurec a实体类
WEB_Kullaniscurec和WEB_ROLSUREC之间的关系由SurecID提供
public partial class WEB_KULLANICISUREC
{
public int KullaniciSurecId { get; set; }
public Nullable<System.Guid> Damga { get; set; }
public Nullable<int> SurecId { get; set; }
public Nullable<int> RolId { get; set; }
public Nullable<bool> OnayDurumu { get; set; }
public Nullable<bool> SurecComplate { get; set; }
public Nullable<bool> SurecAktif { get; set; }
public Nullable<bool> RedDurumu { get; set; }
public string Aciklama { get; set; }
public Nullable<System.DateTime> Tarih { get; set; }
public Nullable<int> IDonaypersonel { get; set; }
public virtual WEB_ROL WEB_ROL { get; set; }
public virtual WEB_KULLANICI WEB_KULLANICI { get; set; }
public virtual WEB_SURECTANIM WEB_SURECTANIM { get; set; }
}
public partial class WEB_SURECROL
{
public int SurecRolID { get; set; }
public Nullable<int> SurecID { get; set; }
public Nullable<int> RolId { get; set; }
public Nullable<bool> OnayDurumu { get; set; }
public Nullable<bool> SurecTamam { get; set; }
public Nullable<bool> SurecAktif { get; set; }
public string surecrol_eposta { get; set; }
public Nullable<System.Guid> WEB_SURECROL_SURECTANIM_Guid { get; set; }
public virtual WEB_ROL WEB_ROL { get; set; }
public virtual WEB_SURECTANIM WEB_SURECTANIM { get; set; }
}
Lambda生成这个T-SQL查询
问题是,;因为它使用内部联接,所以不会返回所有值在桌子上
{SELECT
1 AS [C1],
[Extent4].[SurecAdi] AS [SurecAdi],
[Extent4].[SurecTip] AS [SurecTip],
[Extent3].[Damga] AS [Damga],
[Extent3].[OnayDurumu] AS [OnayDurumu],
[Extent5].[RolAdi] AS [RolAdi],
[Extent3].[SurecComplate] AS [SurecComplate],
[Extent3].[SurecAktif] AS [SurecAktif],
[Extent3].[RedDurumu] AS [RedDurumu],
[Extent3].[Aciklama] AS [Aciklama],
[Extent6].[GirisAdi] AS [GirisAdi],
[Extent3].[Tarih] AS [Tarih],
[Extent3].[IDonaypersonel] AS [IDonaypersonel]
FROM (SELECT DISTINCT
[Extent1].[Damga] AS [Damga],
[Extent1].[SurecId] AS [SurecId]
FROM [dbo].[WEB_KULLANICISUREC] AS [Extent1]
WHERE [Extent1].[Damga] = @p__linq__0 ) AS [Distinct1]
INNER JOIN (SELECT [Extent2].[SurecID] AS [SurecID], [Extent2].[RolId] AS [RolId]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
LEFT OUTER JOIN [dbo].[WEB_SURECROL] AS [Extent2] ON 1 = 1 ) AS [Join1] ON ([Distinct1].[SurecId] = [Join1].[SurecID]) OR (([Distinct1].[SurecId] IS NULL) AND ([Join1].[SurecID] IS NULL))
INNER JOIN [dbo].[WEB_KULLANICISUREC] AS [Extent3] ON (([Distinct1].[Damga] = [Extent3].[Damga]) OR (([Distinct1].[Damga] IS NULL) AND ([Extent3].[Damga] IS NULL))) AND (([Join1].[RolId] = [Extent3].[RolId]) OR (([Join1].[RolId] IS NULL) AND ([Extent3].[RolId] IS NULL)))
LEFT OUTER JOIN [dbo].[WEB_SURECTANIM] AS [Extent4] ON [Join1].[SurecID] = [Extent4].[SurecId]
LEFT OUTER JOIN [dbo].[WEB_ROL] AS [Extent5] ON [Join1].[RolId] = [Extent5].[RolId]
LEFT OUTER JOIN [dbo].[WEB_KULLANICI] AS [Extent6] ON [Extent3].[IDonaypersonel] = [Extent6].[KullaniciId]
WHERE [Extent3].[Damga] = @p__linq__1}
我如何让他使用LeftJoin
surecModel = Surecler.Select(s=> new { s.SurecId , s.Damga})
.Distinct()
//
.Join(
db.WEB_SURECROL.DefaultIfEmpty(),
SURECDAMGALAR => SURECDAMGALAR.SurecId,
SURECROLLER => SURECROLLER.SurecID,
(SURECDAMGALAR, SURECROLLER) => new { SURECDAMGALAR, SURECROLLER })
//
.GroupJoin(Surecler.DefaultIfEmpty(),
surecTanim => new { surecTanim.SURECDAMGALAR.Damga,surecTanim.SURECROLLER.RolId },
SUREC_LIST => new { SUREC_LIST.Damga,SUREC_LIST.RolId },
(surecTanim,SUREC_LIST) => new {surecTanim,SUREC_LIST }
)
//
.Select(x=> new {
x.surecTanim.SURECROLLER.WEB_SURECTANIM.SurecAdi,
x.surecTanim.SURECROLLER.WEB_SURECTANIM.SurecTip,
x.SUREC_LIST.FirstOrDefault().Damga,
x.SUREC_LIST.FirstOrDefault().OnayDurumu,
x.surecTanim.SURECROLLER.WEB_ROL.RolAdi,
x.SUREC_LIST.FirstOrDefault().SurecComplate,
x.SUREC_LIST.FirstOrDefault().SurecAktif,
x.SUREC_LIST.FirstOrDefault().RedDurumu,
x.SUREC_LIST.FirstOrDefault().Aciklama,
x.SUREC_LIST.FirstOrDefault().WEB_KULLANICI.GirisAdi,
x.SUREC_LIST.FirstOrDefault().Tarih,
x.SUREC_LIST.FirstOrDefault().IDonaypersonel,
});
解决了
如果您有不同的建议,我会使用它。您使用的是实体类还是SQLClient类?根据您与数据库的连接,解决方案有所不同。我使用的是实体,此T-SQL解决了我的问题,但我尝试转换为lambda,不幸的是,我没有成功
surecModel = Surecler.Select(s=> new { s.SurecId , s.Damga})
.Distinct()
//
.Join(
db.WEB_SURECROL.DefaultIfEmpty(),
SURECDAMGALAR => SURECDAMGALAR.SurecId,
SURECROLLER => SURECROLLER.SurecID,
(SURECDAMGALAR, SURECROLLER) => new { SURECDAMGALAR, SURECROLLER })
//
.GroupJoin(Surecler.DefaultIfEmpty(),
surecTanim => new { surecTanim.SURECDAMGALAR.Damga,surecTanim.SURECROLLER.RolId },
SUREC_LIST => new { SUREC_LIST.Damga,SUREC_LIST.RolId },
(surecTanim,SUREC_LIST) => new {surecTanim,SUREC_LIST }
)
//
.Select(x=> new {
x.surecTanim.SURECROLLER.WEB_SURECTANIM.SurecAdi,
x.surecTanim.SURECROLLER.WEB_SURECTANIM.SurecTip,
x.SUREC_LIST.FirstOrDefault().Damga,
x.SUREC_LIST.FirstOrDefault().OnayDurumu,
x.surecTanim.SURECROLLER.WEB_ROL.RolAdi,
x.SUREC_LIST.FirstOrDefault().SurecComplate,
x.SUREC_LIST.FirstOrDefault().SurecAktif,
x.SUREC_LIST.FirstOrDefault().RedDurumu,
x.SUREC_LIST.FirstOrDefault().Aciklama,
x.SUREC_LIST.FirstOrDefault().WEB_KULLANICI.GirisAdi,
x.SUREC_LIST.FirstOrDefault().Tarih,
x.SUREC_LIST.FirstOrDefault().IDonaypersonel,
});