Reputation: 69
I have a list which I want to sort and show the entries for a specific date and time.
public class Foodlist
{
public int C_ID { get; set; }
public string DateofFood{ get; set;}
public string FoodTime{ get; set;}
public string FoodDetail{ get; set;}
}
currently I am using the following code to show the list in descending order by date. The date format is same as that of the date picker.
var datetime = foodItems.OrderBy(x => x.DateofFood).ToList();
listBox1.ItemsSource = datetime;
I want to show only the entries for a specific date, say Today. Any ideas?
Upvotes: 1
Views: 567
Reputation: 2569
var datetime = foodItems.Where(x => x.DateofFood == DateTime.Now).ToList();
Upvotes: 0
Reputation: 3444
If you are storing it as string, then it might be a better option to store it as yyyyMMdd. As this will provide you simple sorting.
While you can always do System.DateTime.UtcNow.ToString("yyyyMmMdd")
for comparison with above list.
Upvotes: 0
Reputation: 52808
You can use .Where
to filter the data:
listBox1.ItemsSource = orderedList;
var datetime =
foodItems
.Where(x => DateTime.Parse(x.DateofFood).Date == datePickerValue.Date) //Or whatever the date time is you're after.
.OrderBy(x => x.DateofFood)
.ToList();
It might be better to make the DateofFood
a DateTime
in your class though to avoid using DateTime.Parse()
(or ParseExcact()
if needed).
.Where(x => x.DateofFood.Date == datePickerValue.Date) //Or whatever the date time is you're after.
Notice the use of .Date
to remove the Time component of the DateTime
so you can find entries on a given date.
Upvotes: 2
Reputation: 22565
You can use Date
property of DateTime structure:
var result = foodItems.OrderBy(x => x.DateofFood)
.Where(x=>x.DateofFood.Date == givenDate.Date).ToList();
Upvotes: 1
Reputation: 10900
U need to apply predicate in where like
var datetime = foodItems.Where(x=>x.DateofFood==DateTime.Today).ToList();
Upvotes: 0