简体   繁体   中英

Translate SQL Query to LINQ with left join and let

I've been trying to translate this query to linq for a few days, but I don't get it. I use .NET Framework 4.5 with Code First.

DECLARE @FECHAINICIAL DATETIME, @FECHAFINAL DATETIME
DECLARE @CANAL VARCHAR(300)

SET @FECHAINICIAL = '2019-07-01 17:00'
SET @FECHAFINAL       = '2019-08-07 18:00'
SET @CANAL            = 'channel';

WITH CATEGORIA AS(
       SELECT DISTINCT sm_categoria FROM [DataBase].[dbo].[SM_App] WHERE sm_canal = @CANAL
)

SELECT 
       CATEGORIA.sm_categoria,
       COUNT(CASE WHEN sm_red = 'Facebook' THEN 1 END) as Facebook,
       COUNT(CASE WHEN sm_red = 'Twitter' THEN 1 END) as Twitter,
       COUNT(CASE WHEN sm_red = 'Instagram' THEN 1 END) as Instagram

FROM 
       CATEGORIA
       LEFT JOIN [DataBase].[dbo].[SM_App] ON CATEGORIA.sm_categoria = SM_App.sm_categoria 
                    AND sm_canal like 'channel'
                    AND sm_fecha BETWEEN @FECHAINICIAL AND @FECHAFINAL

group by CATEGORIA.sm_categoria 

CTE is not supported in entity framework. But i guess CTE is not required in your case .Group by is enough is your case and also you don't need a left join . If you still want to use CTE (as in CATEGORIA in your case) you can consider the above answer

DateTime fechaInicial=  new DateTime(2019, 7, 1, 17, 0, 0);
DateTime fechaFinal=  new DateTime(2019, 7, 1, 17, 0, 0);
string canal = "channel";

 var result = (from cat in ctx.CATEGORIAS
                where cat.sm_canal  == canal && cat.sm_fecha >= fechaInicial && cat.sm_fecha <= fechaFinal
                group cat  by cat.sm_categoria  into g
                      select new YourCustomClass { 
                                  sm_categoria= g.Key, 
                                  Facebook = g.Where(p=> p.sm_red  =="Facebook").Count(),
                                  Twitter = g.Where(p=> p.sm_red  =="Twitter").Count(),
                                  Instagram = g.Where(p=> p.sm_red  =="Instagram").Count()
                                  }).ToList();

public class YourCustomClass {

    public string  sm_categoria { get; set; }
    public int Facebook { get; set; }
    public int Twitter { get; set; }
    public int Instagram { get; set; }
}

There's actually a way you can run sql within the entity framework.

this.context.Database.SqlQuery(sqlstatement,params);

The example below would invoke a stored procedure that returns data, which seems like what you're trying to accomplish.

context.Database.SqlQuery<myEntityType>(
"storedprocname @FECHAINICIAL, @FECHAFINAL, @CANAL",
new SqlParameter("FECHAINICIAL", new DateTime(2019, 7, 1, 17, 0, 0)),
new SqlParameter("FECHAFINAL", new DateTime(2019, 8, 7, 18, 0, 0)),
new SqlParameter("CANAL", "channel")

);

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