在同一表中具有条件条件的Linq,并在该条件中从其他表计数

时间:2019-04-05 04:42:21

标签: c# entity-framework linq entity-framework-core

我正在尝试在以下情况下编写linq查询。我正在尝试让Guid没有发送任何消息。

我有3张桌子

TableTarget TargetGuid |FrequencyID 1234 |1 0987 |2

表格频率 FrequencyID|FrequencyTypeID |Time1|Time2 1 |2 |8 AM | 12 PM 2 |1 |2 PM |null

TableSentHistory

发送目标后,此表将更新。

TargetGuid|SentTime 1234 |2019-04-04 08:05:06 1234 |2019-04-04 12:05:06 0987 |2019-04-04 02:05:06

现在,我想提取数据,其中time1或time2>当前时间,并且如果频率表中的频率为1,则今天日期的Tablesenthistory中的计数应小于2;如果频率表中的频率为2,则计数为<3。频率表。 现在,我将从目标和频率表中提取所有数据,并在内存中运行。这是导致我出现性能问题的原因,因为我要拉动所有引导并检查是否存在条件。

是否有一种方法可以通过检查已发送的历史记录来仅获取今天未发送的邮件。

在多台服务器上运行时,我也面临着竞争状况的问题。 这是我的linq查询。

     var targets = (from dt in _context.Target
                              join df in context.Frequency on dt.FrequencyId equals df.FrequencyId                                  
                              where df.Time1.TimeOfDay < DateTime.UtcNow.TimeOfDay
                                    || (df.Time2.TimeOfDay < DateTime.UtcNow.TimeOfDay
                                        && df.Time2 != System.Data.SqlTypes.SqlDateTime.MinValue.Value.ToUniversalTime())
                              select new Processing
                              {
                                 TargetGuid = dt.TargetGUID,
                                  Time1 = df.Time1,
                                  Time2 = df.Time2,
                                  FrequencyType = (FrequencyType)df.FrequencyTypeId
                              }).ToList();

我也尝试添加第三个表联接,但是无法获得计数和大小写比较。

解决方案发布后,我尝试了此操作,但出现此错误。 可空对象必须具有值。

var history = (from ds in context.ServiceHistory
                           where ds.SentTime.Date == DateTime.UtcNow.Date
                           group ds by ds.TargetGuid into grp
                           select new
                           {
                               TargetGuid = grp.Key,
                               Count = grp.Count()
                           }).ToList();

            var Targets = (
                            from dt in context.Target
                            join df in context.Frequency
                            on dt.FrequencyId equals df.FrequencyId
                            join h in history on dt.TargetGUID equals h.TargetGuid into hist
                            from h in hist.DefaultIfEmpty()
                            where df.Time1.TimeOfDay < DateTime.UtcNow.TimeOfDay
                                                    || (df.Time2.TimeOfDay < DateTime.UtcNow.TimeOfDay
                                                        && df.Time2 != System.Data.SqlTypes.SqlDateTime.MinValue.Value.ToUniversalTime())                                
                            && h.Count < (df.FrequencyTypeId == (int)FrequencyType.Daily ? 2 : 3)
                            select new
                            {
                                dt.TargetGUID,
                                df.FrequencyTypeId
                            }).ToList();

1 个答案:

答案 0 :(得分:0)

尝试一下:

var targets = (
    from dt in _context.Target
    join df in context.Frequency 
    on dt.FrequencyId equals df.FrequencyId                                  
    where df.Time1.TimeOfDay < DateTime.UtcNow.TimeOfDay
    || (df.Time2 != null && df.Time2.TimeOfDay < DateTime.UtcNow.TimeOfDay)
    join h in context.SentHistory.Where(hh => hh.SentTime < DateTime.UtcNow.Date)
    on dt.TargetGuid equals h.TargetGuid into history
    where history.Count() < (df.Time2 == null ? 2 : 3)
    select new Processing {
        TargetGuid = dt.TargetGUID,
        Time1 = df.Time1,
        Time2 = df.Time2,
        FrequencyType = (FrequencyType)df.FrequencyTypeId
    }).ToList();