简体   繁体   中英

C# Linq equivalent for SQL script not responding

I have the following SQL script which works fine and pretty fast:

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). 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:

            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. 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. 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!

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