从同一个表合并2个查询到一个LINQ查询
我有2个查询是查询同一个表,但使用不同的参数,我想结合成一体, 查询1从同一个表合并2个查询到一个LINQ查询
//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
OR(||
),不和(&&
)
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)));
OP:在此注意括号是很重要的。 OR的每一侧都包裹在parens中,因为不这样做会有很多不同的结果,因为WITHIN每一侧都是AND运算符。 –
或(||)不是我的意图我需要两个结果 –
@AssafOur在你的评论上面(到你原来的问题)你说你想要返回3件物品。这就是OR在这里给你的。它会给你满足查询1(1项)或查询2(2项)的项目,这意味着它会给你1项+ 2项= 3项。 – Alex
难道你们就不能使用where子句中使用OR条件?这样你可以把你的言论作为一个整体吗? – mahlatse
嗨,在我的查询中,我希望得到两个结果(q1和q2 -3项目总数),而不是其中一个查询的结果。 –