简体   繁体   English

C#Linq等效于SQL脚本没有响应

[英]C# Linq equivalent for SQL script not responding

I have the following SQL script which works fine and pretty fast: 我有以下SQL脚本,可以正常且快速地运行:

select top 30 CONVERT(date, p.DateCreated) as Fecha,
(select count(*) from People d where d.recipientid = p.recipientid and d.SubscriptionType = 0 and CONVERT(date, p.DateCreated) = CONVERT(date, d.DateCreated)) as Subscribed
from People p
where p.RecipientId = '276643679047355'
group by CONVERT(date, p.DateCreated), p.RecipientId
order by CONVERT(date, p.DateCreated) desc;

However, when I'm trying to call this from a C# application using LinQ, it doesn't respond as expected (as a matter of fact, after waiting 5min, I must say it doesn't respond at all). 但是,当我尝试使用LinQ从C#应用程序调用此函数时,它没有按预期方式响应(事实上,等待5分钟后,我必须说它根本没有响应)。 I came up with the following LinQ command, nevertheless, something has to be wrong because its not responding as fast as the SQL script provided: 我想出了以下LinQ命令,但是由于它的响应速度不如所提供的SQL脚本快,所以必须出错了:

            model = await _context.People
                .Where(x => x.RecipientId == recipientId && x.DateCreated > startDate && x.DateCreated < endDate)
                .Select(x => new { DateGrouped = x.DateCreated.ToString("yyyy-MM-dd"), x.RecipientId })
                .GroupBy(x => new { x.DateGrouped, x.RecipientId })
                .Select(a => new StatsViewModel
                {
                    DateStatsFormatted = a.Key.DateGrouped,
                    Subscribed = _context.People.Where(d => d.RecipientId == a.Key.RecipientId && d.SubscriptionType == SubscriptionType.Suscribed && d.DateCreated.ToString("yyyy-MM-dd") == a.Key.DateGrouped).Count()
                }
                )
                .ToListAsync();

Could you please help me to point out what I'm doing wrong or, at least, suggest me what to search? 您能否帮助我指出我做错了什么,或者至少建议我搜索什么?

Well, after a long afternoon trying different approaches, I've finally figured it out. 好了,经过一个漫长的下午尝试不同的方法之后,我终于弄清楚了。 I couldn't get to work my SQL profiler because my database is on Azure and it seems its not compatible. 我无法使用SQL事件探查器,因为我的数据库位于Azure上,并且似乎不兼容。 Date formatting wasn't my solution neither. 日期格式也不是我的解决方案。 The issue was on my nested query for "Subscribed" property... it seems it was creating a sort of circular reference. 问题出在我对“ Subscribed”属性的嵌套查询中……似乎正在创建某种循环引用。 Here is the way is works now: 这是现在可行的方法:

            model = await _context.People
                .Where(x => x.RecipientId == recipientId && x.DateCreated > startDate && x.DateCreated < endDate)
                .Select(x => new { DateGrouped = x.DateCreated.ToString("yyyy-MM-dd"), x.RecipientId, x.SubscriptionType })
                .GroupBy(x => new { x.DateGrouped, x.RecipientId })
                .Select(a => new StatsViewModel
                {
                    DateStatsFormatted = a.Key.DateGrouped,
                    Subscribed = a.Count(c=>c.SubscriptionType == SubscriptionType.Suscribed),
                    Unsubscribed = a.Count(c=>c.SubscriptionType == SubscriptionType.Unsuscribed),
                    NoSet = a.Count(c=>c.SubscriptionType == SubscriptionType.NoSet)
                }
                )
                .ToListAsync();

Notice that I've only added a property "SubscriptionType" on my main select in order to use it later on the filtered select as part of the filter of the "Subscribed" property... pretty simple, straight forward and works like a charm! 请注意,我仅在主选择上添加了属性“ SubscriptionType”,以便稍后在过滤后的选择上将其用作“ Subscribed”属性的过滤器的一部分……非常简单,直接并且像魅力一样工作!

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

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