N
N
neitoo2022-01-27 13:18:26
C++ / C#
neitoo, 2022-01-27 13:18:26

How to implement the output of goods from the List that have expired?

There is a list of goods, for example

List<Product> tovar = new List<Product>()
            {
                new Product(){id = 2,name = "Яблоки",dataproizv = "11.11.2021",cpisanie = "Свежие яблоки.",cena = 100,skidka = 5, kategorya = "Овощи"}
                new Product(){id = 2,name = "Молоко",dataproizv = "16.10.2021",cpisanie = "Свежее молоко.",cena = 80,skidka = 10, kategorya = "Кисломолочные"}
            };


It is necessary to display goods that have expired (3 months for storage of fermented milk products)
I thought to do it through the method, but I don’t understand how to transfer the value from the list or the entire list to it .. (If necessary, I’ll throw off the method)
Or, iterate over the list with the condition, For example:
for (int i = 0; i < tovar.Count; i++)
            {
                //Тут написать условие
                Console.WriteLine("ID " + tovar[i].id + ": \"" + tovar[i].name + "\"" +" | Категория: "+ tovar[i].kategorya + " | Дата производства: " + tovar[i].dataproizv + " | " + tovar[i].cena + " RUB | Скидка " + tovar[i].skidka + " %  " + "\nОписание товара: " + tovar[i].cpisanie);
            }


but again, my dates are stored in strings ...
Maybe there are other solutions, or tell me what to do with these options?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Korotenko, 2022-01-27
@neitoo

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace ConsoleApp2
{
    /// <summary>
    /// Не используйте транслит! Любой китаец или индус поймет английский, 
    /// а вот русский только поржет. 
    /// </summary>
    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string ImportDate { get; set; }
        public string Description { get; set; }
        /// <summary>
        /// Рубли умноженные на 10000. помним об этом то есть у вас копейка имеет 2знака после запятой!!!!
        /// </summary>
        public Int64 Price { get; set; }
        public int Discount { get; set; }
        public string Category { get; set; }
        /// <summary>
        /// Дата списания
        /// </summary>
        public string DueDate { get; set; }
        public string ДляОсобоУпоротыхЭтоТакиРаботает { get; set; }
    }
    public static class Extensions
    {
        /// <summary>
        /// Convert string from format dd.MM.yyyy to DateTime
        /// </summary>
        /// <param name="inputDate"></param>
        /// <returns></returns>
        public static DateTime ParseDateDayMounthYear(this string inputDate) =>
            DateTime.ParseExact(inputDate, "dd.MM.yyyy", CultureInfo.InvariantCulture);
    }
    class Program
    {
        static void Main(string[] args)
        {
            #region заполняем список товаров
            List<Product> tovar = new List<Product>()
            {
                new Product(){Id = 2,
                    Name = "Яблоки",
                    ImportDate = "11.11.2022",
                    Description = "Свежие яблоки.",
                    Price = 1000000,
                    Discount = 5, 
                    DueDate="11.11.2022",
                    Category = "Овощи"},
                new Product(){Id = 2,Name = "Молоко",
                    ImportDate = "16.10.2021",
                    DueDate="16.10.2021",
                    Description = "Свежее молоко.",
                    Price = 800000,
                    Discount = 10, 
                    Category = "Кисломолочные"}
            };
            #endregion

            // число месяц год
            var data =  tovar.Where(item => item.DueDate.ParseDateDayMounthYear() < DateTime.Today).ToList();
        }
    }
}

S
Sergey, 2022-01-27
@werwolflg

Make the date a date, and then such a selection

tovar.Where(item=> (item.dataproizv.AddDays(21)) < DateTime.Today );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question