找到共同的日期时间从日期时间的列表,如果没有共同的然后找到最常见的日期、时间、如果没有、最常见

2023-09-11 23:08:40 作者:逾期

我要找到共同的的DateTime 的DateTime 在我的数据库中可用列表。

I want to find common DateTime from available list of DateTime in my database.

背景

让我们假设,

我想,以满足一些民族,我说我想见见日期时间X之间的某些民族(2014年2月16日09:00:00.000)为DateTime Y(2014年2月26日05:00:00.000)。

I want to meet some peoples, and I say I want to meet certain peoples between Datetime X(2014-02-16 09:00:00.000) to DateTime Y(2014-02-26 05:00:00.000).

然后我想用满足会回答说,我将在以下日期可用的人民:日期1(某些起始时间以一定的结束时间特定日期),DATE2(特定日期的特定时间,特定的时间),.. 。并依此类推。

Then the peoples i want to meet with will reply saying i will be available in following dates: Date1(Certain date from certain start time to certain end time), Date2(certain date from certain time to certain time),...and so on.

让我们考虑这些都是回应

Lets consider these are the responses

Attendee1 (有些GuidId):

Attendee1(Some GuidId):

Response1 :开始时间= 2014年2月23日09:00,结束时间= 2014年2月17日上午11:00,

Response1: Start Time=2014-02-23 09:00 AM, EndTime = 2014-02-17 11:00 AM,

响应2 :开始时间= 2014年2月24日上午10点00分,结束时间= 2014年2月17日12:00 PM,

Response2 : Start Time=2014-02-24 10:00 AM, EndTime = 2014-02-17 12:00 PM,

Response3 :开始时间= 2014年2月25日上午10:00,结束时间= 2014年2月17日上午11:00,

Response3 : Start Time=2014-02-25 10:00 AM, EndTime = 2014-02-17 11:00 AM,

Response4 :开始时间= 2014年2月23日下午1时零零,结束时间= 2014年2月17日下午5:00

Response4 : Start Time=2014-02-23 01:00 PM, EndTime = 2014-02-17 5:00 PM

Attendee2 (有些GuidId):

Attendee2(Some GuidId):

Response1 :开始时间= 2014年2月22日09:00,结束时间= 2014年2月17日17:00,

Response1: Start Time=2014-02-22 09:00 AM, EndTime = 2014-02-17 05:00 PM,

响应2 :开始时间= 2014年2月23日09:00,结束时间= 2014年2月17日17:00,

Response2 : Start Time=2014-02-23 09:00 AM, EndTime = 2014-02-17 05:00 PM,

Response3 :开始时间= 2014年2月25日09:00,结束时间= 2014年2月17日下午12:00,

Response3 : Start Time=2014-02-25 09:00 AM, EndTime = 2014-02-17 12:00 PM,

Attendee3 (有些GuidId):

Attendee3(Some GuidId):

Response1 :开始时间= 2014年2月22日上午11:00,结束时间= 2014年2月17日02:00 PM,

Response1: Start Time=2014-02-22 11:00 AM, EndTime = 2014-02-17 02:00 PM,

响应2 :开始时间= 2014年2月23日下午4时00,结束时间= 2014年2月17日03:00 PM,

Response2 : Start Time=2014-02-23 04:00 PM, EndTime = 2014-02-17 03:00 PM,

Response3 :开始时间= 2014年2月23日下午4时00,结束时间= 2014年2月17日17:00,

Response3 : Start Time=2014-02-23 04:00 PM, EndTime = 2014-02-17 05:00 PM,

Response4 :开始时间= 2014年2月24日02:00,结束时间= 2014年2月17日17:00,

Response4 : Start Time=2014-02-24 02:00 AM, EndTime = 2014-02-17 05:00 PM,

Response5 :开始时间= 2014年2月25日上午11:00,结束时间= 2014年2月17日下午12:00,

Response5 : Start Time=2014-02-25 11:00 AM, EndTime = 2014-02-17 12:00 PM,

在这里,在上述情况下,系统应提供配套的日期为:

Here, in above scenario, system should provide matching dates as:

2014-02-23 04:00 PM to 2014-02-23 05:00 PM 

2014-02-25 11:00 AM to 2014-02-25 12:00 PM

因此​​,一旦每个人都已经回答。我想找到共同的的DateTime 或共同名单,其中,日期时间> (如果存在)。

So once every one has replied. I would like to find the common DateTime or common List<DateTime>(if exists).

或者找到日期时间(列表),有人数最多的比赛

我知道我必须说明我曾尝试和我做了什么,但我不知道在我的脑海,我应该如何开始。

I know I have to show what I have tried and what I have done, but I have no idea in my mind how should I start.

任何一种建议或暗示将AP preciated。

Any kind of suggestions or hints would be appreciated.

修改1

因此​​,在数据库中有表名为预约存储的 StartDateTime EndDateTime

So in database there is table called Appointment which stores the StartDateTime and EndDateTime

public class Appointment
{

    [Key]
    public Guid Id { get; set; }

    public virtual ICollection<Attendee> Attendees { get; set; }

    public DateTime StartDateTime { get; set; }
    public DateTime EndDateTime { get; set; }      

}

和这些日期的人(与会者)出席会给他们的回应之间。 每个人(谁将会作出反应),其信息存储在名为数据库表与会者

and between these dates people(Attendee) attending will give their response. Each person(who will respond), their information is stored in database table called Attendees

public class Attendee
{
    public Guid AttendeeId { get; set; }
    public virtual ICollection<Response> Responses { get; set; } 
}

和每个用户的响应存储在响应表,其模式将类似于

And for each user their response is stored in Responses table whose model would look like

public class Response
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public Guid AttendeeId { get; set; }
    public DateTime StartDateTime { get; set; }
    public DateTime EndDateTime { get; set; }

}

PS : 这是一个 MVC应用程序并使用创建数据库 code第一种方式

PS: It is a MVC application and database is created using code first approach.

此外,将是有意义的增加 AppointmentId 响应表?或者会说是坏的做法?但我认为这将让查询变得更加容易。

Also, would it make sense to add AppointmentId to Response table? or would that be bad approach? But I think it would make the querying easier.

推荐答案

这是可能的工作的一种方法:

An approach that might work:

计算组合的列表,每个人都可以参加。 您将开始与用于第一参加者的响应,以及​​然后通过其他与会者枚举。 当你枚举,对每一个可能的时间仍然可以正常工作,缩小时间窗口,以适应所有的时间,所有可能的与会者可以使用

可能需要一些感人,以处理边缘情况,但下面的code应该让你在正确的道路上:

Might need some touching up to handle edge cases, but the following code should get you on the right path:

public class MeetingTime 
{
  DateTime Start { get; set; }
  DateTime End { get; set; }
  List<Response> Responses { get; set; }
  public int NumAttendees 
  {        
    get { return Responses.Select(x => x.AttendeeId).Distinct().Count(); }
  }

  public MeetingTime(Response response) 
  {
    Start = response.StartDateTime;
    End = response.EndDateTime;
    Responses = new List<Response>();
  }

  public void MergeMeetingTime(Response response) 
  {
    // if response times are within current times, can be merged
    if (Start <= response.StartDateTime && response.EndDateTime <= End)
    {
       Start = response.StartDateTime;
       End = response.EndDateTime;
       Responses.Add(response);
    }
  }
}

public List<MeetingTime> FindMeetingTimes() 
{
  var attendees = GetAttendees();
  var times = new List<MeetingTime> 
  bool isFirstAttendee = true;
  foreach (Attendee attendee in attendees) 
  {
    if (isFirstAttendee) 
    {
      foreach (Response response in attendee.Responses)
      {
        times.Add(new MeetingTime(response));
      }
      isFirstAttendee = false;
    } 
    else 
    {
      // Go through attendee.Responses and compare each with the current times
      // for each one, if it overlaps with a MeetingTime, then adjust 
      // the Start/End dates accordingly and increment Count
      // Uses same approach as above.          
      foreach (Response response in attendee.Responses)
      {
        times.ForEach(x => x.MergeMeetingTime(response));
      }
    }
  }
  // Remove all times where not everyone can attend
  times.Remove(x => x.NumAttendees < attendees.Count()).ToList();
  return times;
}

最后,删除任何时候都在那里不是每个人都可以参加。所剩下的时间应分别开始/结束DateTime是否所有与会者可以做到的。

At the end, you remove all times where not everyone can attend. The times that are remaining should each have Start/End DateTimes that all Attendees can make.