[英]How do I create a unique list of dates in a drop down in ASP MVC with C#?
I'm working with the Entity framework in ASP MVC and I want to pull a list of unique dates from the database that has a datetime field and display them in a drop down. 我正在使用ASP MVC中的Entity框架,我想从具有datetime字段的数据库中提取唯一日期列表,并在下拉列表中显示它们。 I can't find a way to format the output and only list the unique dates in order so whomever is on the page will see something like "8/7/2015", "8/8/2015" instead of "8/7/2015 11:00:00 AM" and "8/7/2015 11:25:00 AM"... for AppointmentStartDateTime
. 我找不到一种格式化输出的方法,只能按顺序列出唯一的日期,这样页面上的任何人都会看到类似“ 8/7/2015”,“ 8/8/2015”而不是“ 8/7”的信息/ 2015 11:00:00 AM”和“ 2015年8月7日11:25:00 AM” ...表示AppointmentStartDateTime
。
Here's my Model so far 到目前为止,这是我的模特
public class Appointment
{
[Key]
[Column("apt_Id")]
[Display(Name = "Appointment Id")]
public int AppointmentId { get; set; }
[Column("apt_apl_Id")]
public int AppealId { get; set; }
public virtual Appeal Appeal { get; set; }
[Column("apt_ofh_Id")]
public int OfficeHoursId { get; set; }
public virtual OfficeHours OfficeHours { get; set; }
[Column("apt_StartDateTime")]
public DateTime AppointmentStartDateTime { get; set; }
[Column("apt_EndDateTime")]
public DateTime AppointmentEndDateTime { get; set; }
}
I'm stuck in the controller in the code shown below: 我被卡在控制器中,如下所示:
SelectList AppointmentDateSelectList = new SelectList(
db.Appointment.Where(g => g.AppointmentStartDateTime > DateTime.Now),
"AppointmentStartDateTime","AppointmentStartDateTime")
.Select(a => new SelectListItem
{
Text = Convert.ToDateTime(a.Text).ToShortDateString(),
Value = a.Value
});
I'm hoping someone knows how to make unique date list out of the start times. 我希望有人知道如何在开始时间之外制作唯一的日期列表。 I haven't found any information on it and I feel like I'm banging my head against a wall. 我还没有找到任何信息,我好像要把头撞在墙上。
Instead of using ToShortDateString()
, use ToString("MM/dd/yyyy")
, or whatever other format you want. 而不是使用ToShortDateString()
,请使用ToString("MM/dd/yyyy")
或您想要的任何其他格式。
NOTE: This would be a comment, but I don't have enough reputation. 注意:这将是一条评论,但是我没有足够的声誉。
Firstly, and a tad off-topic, but I would encourage you to use good formatting. 首先,还有一点点题外话,但我鼓励您使用良好的格式。 You'd be amazed at how often weird formatting makes a piece of code seem more complicated that it is. 您会惊讶于奇怪的格式化使一段代码看起来比现在更复杂的频率。 For this reason, I would also break out the database bit into a separate variable for readability. 因此,为了可读性,我还将数据库位分解为一个单独的变量。
var matchingAppointments = db.Appointment.Where(g => g.AppointmentStartDateTime > DateTime.Now);
Secondly, I think it makes more sense to do the sorting and distinct-ifying against the database portion, not the SelectList
itself: 其次,我认为对数据库部分而不是SelectList
本身进行排序和区分是更有意义的:
// Renamed variable
var distinctAppointmentDates = db.Appointment.Where(appt => appt.AppointmentStartDateTime > DateTime.Now)
.OrderBy(appt => appt.AppointmentStartDateTime)
.ToList() // Force query execution
.Select(appt => appt.AppointmentStartDateTime.ToShortDateString())
.Distinct();
SelectList AppointmentDateSelectList = new SelectList(distinctAppointmentDates);
Lastly, your use of ToShortDateString
is fine so far as I can see. 最后,据我所知,您对ToShortDateString
的使用很好。 I'm not sure why others think that is the issue. 我不确定为什么其他人会认为这是问题所在。
Try creating a list of selectlistitems then put that list in your selectlist 尝试创建选择列表的列表,然后将该列表放入选择列表
var list = db.Appointment.Where(g => g.AppointmentStartDateTime > DateTime.Now)
.select(s => new SelectListItem{
Text = s.AppointmentStartDateTime.ToString("dd/mm/yy"),
Value = a.AppintmentStartDateTime
});
var AppointmentDateSelectList = new SelectList(list, "Text","Value");
To choose unique dates you can use GroupBy()
in your linq query. 要选择唯一的日期,可以在linq查询中使用GroupBy()
。 ToString()
will also help you with date format. ToString()
还将帮助您使用日期格式。
var list = db.Appointment.Where(g => g.AppointmentStartDateTime >DateTime.Now).GroupBy(x=>x.AppointmentStartDateTime)
.select(s => new SelectListItem
{
Text = s.Key.Value.ToString("dd/mm/yy"),
Value = s.Key.Value.ToString()
});
var AppointmentDateSelectList = new SelectList(list, "Text","Value");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.