我在这里有这段代码。我认为这不是很有效,我想知道是否有一种方法可以使.Where子句在开头或结尾处添加,而不是重复三遍。如果有人可以给我任何建议并展示如何做到这一点,我将不胜感激:
List<Phrase> phrases;
switch (options.PhraseTypeSelectId)
{
case 0:
phrases = await db.Phrases
.Where(w => ((w.CategoryId == options.CategorySelectId || options.CategorySelectId == null) ||
(w.ChapterId == options.ChapterSelectId || options.ChapterSelectId == null)))
.Where(w => (w.EnglishAscii >= es1 && w.EnglishAscii <= ee1) || (w.EnglishAscii >= es2 && w.EnglishAscii <= ee2))
.Where(w => (w.RomajiAscii >= rs1 && w.RomajiAscii <= re1) || (w.RomajiAscii >= rs2 && w.RomajiAscii <= ee2))
.Where(w => (options.CreatedBy == 0 || w.CreatedBy == options.CreatedBy))
.Where(w => (options.ModifiedBy == 0 || w.ModifiedBy == options.ModifiedBy))
.Where(w => (options.JLPT == 0 || w.JLPT == options.JLPT))
.AsNoTracking()
.ToListAsync();
return Ok(phrases);
case 1:
phrases = await db.Phrases
.Where(w => ((w.CategoryId == options.CategorySelectId || options.CategorySelectId == null)))
.Where(w => (w.EnglishAscii >= es1 && w.EnglishAscii <= ee1) || (w.EnglishAscii >= es2 && w.EnglishAscii <= ee2))
.Where(w => (w.RomajiAscii >= rs1 && w.RomajiAscii <= re1) || (w.RomajiAscii >= rs2 && w.RomajiAscii <= ee2))
.Where(w => (options.CreatedBy == 0 || w.CreatedBy == options.CreatedBy))
.Where(w => (options.ModifiedBy == 0 || w.ModifiedBy == options.ModifiedBy))
.Where(w => (options.JLPT == 0 || w.JLPT == options.JLPT))
.AsNoTracking()
.ToListAsync();
return Ok(phrases);
case 2:
phrases = await db.Phrases
.Where(w => ((w.ChapterId == options.ChapterSelectId || options.ChapterSelectId == null)))
.Where(w => (w.EnglishAscii >= es1 && w.EnglishAscii <= ee1) || (w.EnglishAscii >= es2 && w.EnglishAscii <= ee2))
.Where(w => (w.RomajiAscii >= rs1 && w.RomajiAscii <= re1) || (w.RomajiAscii >= rs2 && w.RomajiAscii <= ee2))
.Where(w => (options.CreatedBy == 0 || w.CreatedBy == options.CreatedBy))
.Where(w => (options.ModifiedBy == 0 || w.ModifiedBy == options.ModifiedBy))
.Where(w => (options.JLPT == 0 || w.JLPT == options.JLPT))
.AsNoTracking()
.ToListAsync();
return Ok(phrases);
}
return BadRequest();
我想在没有该语句的情况下包括switch语句,它与与PhraseTypeSelectId相关的.Where子句变得非常混乱
最佳答案
由于我无法编译该示例,因此我不确定100%是否可以编译该示例,但是您应该了解它的要旨。
您可以为不同的PhraseTypeSelecteId
创建特定的表达式,并稍后在.Where()
方法中使用它。
Expression<Func<Phrase, bool>> phraseTypePredicate = null;
switch (options.PhraseTypeSelectId)
{
case 0:
phraseTypePredicate =
w => ((w.CategoryId == options.CategorySelectId || options.CategorySelectId == null) ||
(w.ChapterId == options.ChapterSelectId || options.ChapterSelectId == null));
break;
case 1:
phraseTypePredicate =
w => ((w.CategoryId == options.CategorySelectId || options.CategorySelectId == null));
break;
case 2:
phraseTypePredicate =
w => ((w.ChapterId == options.ChapterSelectId || options.ChapterSelectId == null));
break;
}
if (phraseTypePredicate != null)
{
List<Phrase> phrases = phrases = await db.Phrases
.Where(phraseTypePredicate)
.Where(w => (w.EnglishAscii >= es1 && w.EnglishAscii <= ee1) || (w.EnglishAscii >= es2 && w.EnglishAscii <= ee2))
.Where(w => (w.RomajiAscii >= rs1 && w.RomajiAscii <= re1) || (w.RomajiAscii >= rs2 && w.RomajiAscii <= ee2))
.Where(w => (options.CreatedBy == 0 || w.CreatedBy == options.CreatedBy))
.Where(w => (options.ModifiedBy == 0 || w.ModifiedBy == options.ModifiedBy))
.Where(w => (options.JLPT == 0 || w.JLPT == options.JLPT))
.AsNoTracking()
.ToListAsync();
return Ok(phrases);
}
return BadRequest();
关于c# - 有没有办法可以延迟LINQ语句中的.Where子句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39787928/