将来自同一表的2个查询合并为一个linq查询

本文关键字:查询 一个 linq 合并 2个 将来 | 更新日期: 2023-09-27 18:09:07

我有2个查询是查询相同的表,但有不同的参数,我想合并成一个,查询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
查询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

我想创建的查询

 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

我也试过这个

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

将来自同一表的2个查询合并为一个linq查询

(||),而不是 (&&)

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)));