Assaf Our
Assaf Our

Reputation: 639

combine 2 queries from the same table into one linq query

I have 2 queries that are query the same table but with different parameters and I would like to combine into one , Query 1

 //ToDo refactor
        int myDayOfWeek = 0;
        DateTime dt = DateTime.Today;
        myDayOfWeek = (int)dt.DayOfWeek;

        var todaysLecture = (from c in _context.LectureGigs.Where(g => g.IsWeekLy == true && (int)g.Days == (myDayOfWeek))
                             select c).ToList();
        //results Ok 1 result

Query 2

var upCommingLecture = _context.LectureGigs
            .Include(g => g.Artist).Include(g => g.Genre)
            .Where(g => g.DateTime > DateTime.Now && !g.IsCanceled);
        //results Ok 2 result

The Query I would like to create

 var upCommingLecture = _context.LectureGigs
           .Include(g => g.Artist).Include(g => g.Genre).Where(g => g.IsWeekLy == true && (int)g.Days == (myDayOfWeek))
           .Where(g => g.DateTime > DateTime.Now && !g.IsCanceled);
        //Error none but 0 result

I have also tried this

var upCommingLecture = _context.LectureGigs
           .Include(g => g.Artist).Include(g => g.Genre)
           .Where(g => g.DateTime > DateTime.Now && !g.IsCanceled && g.IsWeekLy==true &&(int)g.Days==(myDayOfWeek));
        //Error none but 0 result

Upvotes: 2

Views: 631

Answers (1)

James Curran
James Curran

Reputation: 103485

OR (||), not AND (&&)

var upCommingLecture = _context.LectureGigs
       .Include(g => g.Artist).Include(g => g.Genre)
       .Where(g => (g.DateTime > DateTime.Now && !g.IsCanceled) 
                || (g.IsWeekLy==true &&(int)g.Days==(myDayOfWeek)));

Upvotes: 3

Related Questions