Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有两个表和一组筛选器的实体框架6.1查询_C#_.net_Sql Server_Entity Framework - Fatal编程技术网

C# 具有两个表和一组筛选器的实体框架6.1查询

C# 具有两个表和一组筛选器的实体框架6.1查询,c#,.net,sql-server,entity-framework,C#,.net,Sql Server,Entity Framework,因此,我正在将一个旧项目转换为新代码,下面是删除的实体无关数据 public partial class Event { public Event() { RecurringEvents = new HashSet<RecurringEvent>(); } public int EventId { get; set; } public int ModuleID { get; set; } public DateTi

因此,我正在将一个旧项目转换为新代码,下面是删除的实体无关数据

public partial class Event
{
    public Event()
    {
        RecurringEvents = new HashSet<RecurringEvent>();
    }

    public int EventId { get; set; }

    public int ModuleID { get; set; }

    public DateTime EventDate { get; set; }

    public DateTime CreateDate { get; set; }

    public bool IsActive { get; set; }

    public bool IsRecurring { get; set; }

    public virtual ICollection<RecurringEvent> RecurringEvents { get; set; }
}

public partial class RecurringEvent
{
    public int RecurringEventID { get; set; }

    public int EventID { get; set; }

    public DateTime EventDate { get; set; }

    public virtual Event Event { get; set; }
}
基本上,我需要在EF查询中重写以下SQL语句:

SELECT  e.EventId, e.Title, e.Abstract, ISNULL(re.EventDate, e.EventDate) AS EventDate, e.CreateDate, e.EndDate, e.IsActive, re.RecurringEventID AS RecurringEventId
FROM  Calendar.Events AS e LEFT OUTER JOIN
Calendar.RecurringEvents AS re ON e.EventId = re.EventID
WHERE   (e.ModuleID = 3045) AND (e.IsActive = 1) AND (e.EventDate >= '2015-03-01') AND (e.EventDate < '2015-05-31') OR (re.EventDate >= '2015-03-01') AND (re.EventDate < '2015-05-31')
编辑 这是我提出的当前查询,但它返回的是内部连接而不是左连接

        IEnumerable<EventView> toReturn = _context.Events
            .Join(_context.RecurringEvents.DefaultIfEmpty(),
                e => e.EventId,
                re => re.EventID,
                (e, re) => new {Event = e, RecurringEvent = re})
            .Where(ere =>
                ere.Event.ModuleID == moduleID &&
                ere.Event.IsActive == true &&
                ((ere.Event.EventDate >= startMonth && ere.Event.EventDate < endMonth) ||
                 (ere.RecurringEvent.EventDate >= startMonth && ere.RecurringEvent.EventDate < endMonth)))
            .Select(ere => new EventView()
            {
                EventId = ere.Event.EventId,
                Title = ere.Event.Title,
                Abstract = ere.Event.Abstract,
                EventDate = ere.RecurringEvent.EventDate != null ? ere.RecurringEvent.EventDate : ere.Event.EventDate,
                CreateDate = ere.Event.CreateDate,
                EndDate = ere.Event.EndDate,
                IsActive = ere.Event.IsActive,
                RecurringEventID = ere.RecurringEvent.RecurringEventID
            }).AsEnumerable();
谢谢你的帮助。谢谢

啊,LINQ中臭名昭著的左外连接……您需要使用DefaultIfEmpty


这是这里唯一棘手的部分

添加实际的SQL表DDL。以及您实际尝试的linq代码。calendar类在哪里?它的结构如何您可以尝试类似于日历中的e。事件在日历中加入re。e.EventId上的RecurringEvents等于re.EventId从gj.DefaultIfEmpty中的子集进入gj,其中e.ModuleID==3045&&e.IsActive==true&&e.EventDate.Date>=someDate.Date选择新的{e.EventId,e.CreateDate,e.EventDate};