locked
between two dates as where condition along with GETALL() method RRS feed

  • Question

  • User-1355965324 posted

    I have the following model. I am trying to get all the records of vehicles , the MOT dated coming under  between two dates . I want  to give where condition in GetAll method linq

     public class Vehicles
        {
            [Key]
            public int Id { get; set; }
            [Required]
            public string Make { get; set; }
            [Required]
            public string Model { get; set; }
            [Required]
            public string RegNo { get; set; }
            [Required]
            public string VehicleName {get;set;}
            public DateTime? MOTDate { get; set; }
    }

    Repository

    public IEnumerable<T> GetAll(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = null)
            {
                IQueryable<T> query = dbSet;
    
                if (filter != null)
                {
                    query = query.Where(filter);
                }
    
                if (includeProperties != null)
                {
                    foreach (var includeProp in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        query = query.Include(includeProp);
                    }
                }
    
                if (orderBy != null)
                {
                    return orderBy(query).ToList();
                }
                return query.ToList();
            }

    Controller

    datetime  datefrom 

    datetime dateto

    var VehileList = VehicleRepo.GetAll() // how can I give the where condition  between datefrom  and  dateto,  please help

    Friday, June 19, 2020 9:52 AM

Answers

  • User-1355965324 posted

    I sorted by  giving 

    var result = GetAll(f => f.MOTDate > StarDate && f.MOTDate < EndDate);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 19, 2020 10:26 AM