简体   繁体   English

如何执行LINQ查询以达到以下T-SQL查询的相同结果?

[英]How to perform LINQ query to reach the same result of the following T-SQL query?

I have this SQL query, which works fine for my purposes : 我有这个SQL查询,可以很好地满足我的目的:

SELECT [Documenti].[ID]
    ,[Documenti].[Data]
    ,[Documenti].[Descrizione]
    ,[Documenti].[DAL]
    ,[Documenti].[AL]
    ,[Documenti].[Stato]
    ,[Documenti].[NomeDoc]
    ,[Documenti].[Classe]
    ,[Documenti].[CTipo]
    ,[Documenti].[CSTIpo]
    ,[Documenti].[SiglaRed]
    ,[Documenti].[Stipula]
    ,[Documenti].[DataCmp]
    ,[Documenti].[Regione]
    ,[Documenti].[NumDoc]
    ,[Documenti].[Destinazione]
    ,[Documenti].[DataStampa]
    ,[Documenti].[Ext]
    ,[Tipi].GRP2
FROM [Documenti]
LEFT JOIN Tipi ON Tipi.C = [Documenti].Classe AND Tipi.C_Tipo = [Documenti].CTipo
LEFT JOIN STipi ON STipi.C = [Documenti].Classe AND STipi.C_STipo = [Documenti].CSTipo
where [Documenti].Stato IN (4,2,3)

In particular I need to realize the SELECT IN clause, because without it my LINQ code works fine. 特别是,我需要实现SELECT IN子句,因为没有它,我的LINQ代码就可以正常工作。 My LINQ query is the following : 我的LINQ查询如下:

IQueryable listaDoc = null;
string _fasi = "4;2;3";
var fasi = _fasi.Split(';').Select(n => short.Parse(n)).ToList();
listaDoc = (from DOC in db.Documenti
                        from subdoc in db.Tipi.Where(j => j.C_Tipo == DOC.CTipo && j.C == DOC.Classe.Value).DefaultIfEmpty()
                        from subdoc2 in db.Stipi.Where(j => j.C_STipo == DOC.CSTIpo && j.C == DOC.Classe.Value).DefaultIfEmpty()
                        from subdoc3 in db.PathClassi_Web.Where(j => j.cod_class == DOC.Classe).DefaultIfEmpty()
                        where fasi.Contains(DOC.Stato.Value)
                        select new
                        {
                            DOC.ID,
                            DOC.Data,
                            DOC.Descrizione,
                            DOC.DAL,
                            DOC.AL,
                            DOC.Stato,
                            DOC.NomeDoc,
                            DOC.Classe,
                            DOC.CTipo,
                            DOC.CSTIpo,
                            DOC.SiglaRed,
                            DOC.Stipula,
                            DOC.DataCmp,
                            DOC.Regione,
                            DOC.NumDoc,
                            DOC.Destinazione,
                            DOC.DataStampa,
                            DOC.Ext,
                            GRP2 = subdoc == null ? string.Empty : subdoc.GRP2,
                            DESCRIZ = subdoc2 == null ? string.Empty : subdoc2.Descriz,
                            DESCR_CLASS = subdoc3 == null ? string.Empty : subdoc3.descr_class
                        });

But it returns to me 0 results. 但这返回给我0个结果。 What's wrong in my LINQ query in order to reach the same result of my pure SQL query described above? 为了达到与上述纯SQL查询相同的结果,我的LINQ查询有什么问题?

I have used the tool Linqer ( http://www.sqltolinq.com/ ) to help me convert complex SQL-statements to linq. 我已经使用工具Linqer( http://www.sqltolinq.com/ )帮助我将复杂的SQL语句转换为linq。 It saved me many times. 它救了我很多次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM