简体   繁体   English

当两个日期时间戳记时,将日期和时间字段合并为一个

[英]Combined to date and time fields into one when both datetime stamps

I am trying to combine the date and time part of a table so that I can set appointments correctly I was wondering if someone could help me my syntax is not being compiled 我正在尝试合并表格的日期和时间部分,以便可以正确设置约会,我想知道是否有人可以帮助我,但我的语法没有被编译

 public List<Appointment> getAppointments (DateTime AppointmentDate)
 {
    List<Appointment> query = _sourceEntities.Appointments.Where(a => a.ApptDate == AppointmentDate && a.ClientID==6).ToList();
    return _sourceEntities.Appointments.Select(r =>

    {
            var newAppointment = new Appointment();
            DateTime date = new DateTime(r.ApptDate.Year, r.ApptDate.Month, r.ApptDate.Day, r.ApptTime.Hour, r.ApptTime.Minute, r.ApptTime.Second);
            newAppointment.ApptDate = date;
            return newAppointment();


    });

}

The error is hapening here return newAppointment(); 该错误正在发生,在这里返回newAppointment(); I am not sure why its saying method name expected I want to have all the fields of the old list but also this new combined date time field. 我不确定为什么它的说法方法名称期望我既要拥有旧列表的所有字段,又要拥有这个新的合并日期时间字段。

Here is the example of the data to best explain 这是最好解释的数据示例

https://i.imgur.com/rCtx0lt.png https://i.imgur.com/rCtx0lt.png

Edit 2 编辑2

The _sourceEntites is decalred heree at the top of the class _sourceEntites在此课程的顶部被移除

public class SourceContext 
{
     public  SMBASchedulerEntities _sourceEntities = new SMBASchedulerEntities();
     public static List<Appointment> getAppointments(DateTime apptDate)
     List<Appointment> query = _sourceEntities.Appointments.Where(a => a.ApptDate == AppointmentDate && a.ClientID==6).ToList();
     return _sourceEntities.Appointments.Select(r =>

    {
            var newAppointment = new Appointment();
            DateTime date = new DateTime(r.ApptDate.Year, r.ApptDate.Month, r.ApptDate.Day, r.ApptTime.Hour, r.ApptTime.Minute, r.ApptTime.Second);
            newAppointment.ApptDate = date;
            return newAppointment();


    });
}

newAppointment is an object variable, by using ('s the compiler treats newAppointment as a method, that is what the error message states. Removing the ('s should solve the problem. newAppointment是一个对象变量,通过使用(的,编译器将newAppointment视为一种方法,这就是错误消息指出的内容。删除()应该可以解决问题。

An alternative way to return the result would be 返回结果的另一种方法是

public List<Appointment> getAppointments (DateTime AppointmentDate)
 {
    List<Appointment> query = _sourceEntities.Appointments.Where(a => a.ApptDate == AppointmentDate && a.ClientID==6).ToList();
    return _sourceEntities.Appointments.Select(r => new Appointment

    {
            newAppointment.ApptDate = ew DateTime(r.ApptDate.Year, r.ApptDate.Month, r.ApptDate.Day, r.ApptTime.Hour, r.ApptTime.Minute, r.ApptTime.Second);
            //map other variables here
    });

}

The problem with your code is in the line: return newAppointment(); 您的代码存在问题: return newAppointment(); . You are treating the object newAppointment like a method when you add the parenthesis after it. 当在对象newAppointment之后添加括号时,您会将其newAppointment一种方法。 Instead, you can just do: 相反,您可以执行以下操作:

return newAppointment;

A slightly simpler approach would be to create a new Appointment in your Select statement. 一种更简单的方法是在Select语句中创建一个新的Appointment This will return an IEnumerable of Appointment objects, which we can the call ToList() on before returning. 这将返回一个IEnumerableAppointment对象,我们可以在返回之前调用ToList() I also included a .Where() clause to match what you had in your query . 我还包括一个.Where()子句,以匹配query You can remove that line if it's not needed. 如果不需要,您可以删除该行。

public static List<Appointment> getAppointments(DateTime apptDate)
{
    return _sourceEntities.Appointments
        .Where(a => a.ApptDate == apptDate && a.ClientID == 6) // Remove if not needed
        .Select(r =>
            new Appointment
            {
                ApptDate = new DateTime(r.ApptDate.Year, r.ApptDate.Month, r.ApptDate.Day,
                    r.ApptTime.Hour, r.ApptTime.Minute, r.ApptTime.Second)
            })
        .ToList();
}

One other thing to note is that you are doing an equality comparison on the two date objects, so you will only be getting appointments for the exact date and time of the argument passed in. In case you want to get all the appointments for the day , you can just use the Date portion of the DateTime objects for comparison: 需要注意的另一件事是,您正在对两个日期对象进行相等比较,因此您将只获得传入参数的确切日期和时间的约会。以防您想要获取当天的所有约会,您可以仅使用DateTime对象的Date部分进行比较:

// Compare the Date portion of the argument to get all appointments for that day
.Where(a => a.ApptDate.Date == appointment.Date && a.ClientID == 6)

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

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