简体   繁体   中英

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 :

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. My LINQ query is the following :

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. What's wrong in my LINQ query in order to reach the same result of my pure SQL query described above?

I have used the tool Linqer ( http://www.sqltolinq.com/ ) to help me convert complex SQL-statements to linq. It saved me many times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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