I wanted to list dates in the dropdown list, but I am having a conversion problem in the GenreLst.AddRange command (GenreQry.Distinct ()); can someone help? thanks
I tried use Convert.ToDateTime() but not work.
public ActionResult Index(string startData)
{
var GenreLst = new List<string>();
var GenreQry = from d in db.Programa
orderby d.Data_Registo
select d.Data_Registo;
GenreLst.AddRange(GenreQry.Distinct()); //Error string
ViewBag.startData = new SelectList(GenreLst);
var datas = from m in db.Programa
select m;
if (string.IsNullOrEmpty(startData))
return View(datas);
else
{
DateTime start = Convert.ToDateTime(startData);
return View(datas.Where(x => x.Data_Registo == start));
}
}
CS1503:Argument 1: cannot convert from 'System.Linq.lQueryable<DateTime?> to System.Collections.Generic.lEnumerable<string>'
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
It looks like you’re attempting to append a list of type DateTime? to a list of type string which is causing the error you are seeing here.
To resolve this you could declare GenreLst to be of type List<DateTime?> such as:
var GenreLst = new List<DateTime?>();
Which would then allow you to append GenreQry.
Alternatively could declare GenreLst as the result from GenreQry.Distinct():
var GenreLst = GenreQry.Distinct();
Method 2
public static DateTime? ToNullableDateTime(object dateTime)
{
if (dateTime == null)
{
return null;
}
DateTime tvParsedDate;
if (DateTime.TryParse(dateTime.ToString(), out tvParsedDate))
{
return tvParsedDate;
}
return null;
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0