使用LINQ接收属性为true的所有活动数据
我首先使用带有EF模型的MVC3 我有这个LINQ来接收所有数据:使用LINQ接收属性为true的所有活动数据,linq,entity-framework,lambda,Linq,Entity Framework,Lambda,我首先使用带有EF模型的MVC3 我有这个LINQ来接收所有数据: public List<CoreValueQuestion> GetAllCoreValueQuestions() { return db.Question.OfType<CoreValueQuestion>().OrderBy(x => x.QuestionText).ToList(); } public List GetA
public List<CoreValueQuestion> GetAllCoreValueQuestions()
{
return db.Question.OfType<CoreValueQuestion>().OrderBy(x => x.QuestionText).ToList();
}
public List GetAllCoreValueQuestions()
{
返回db.Question.OfType().OrderBy(x=>x.QuestionText.ToList();
}
我的问题实体有一个属性是bool,称为Active,我想返回所有Active=true的问题。我该怎么做
提前谢谢 可能是这样的:
db.Question
.OfType<CoreValueQuestion>()
.Where(a=>a.Active==true)
.OrderBy(x => x.QuestionText)
.ToList();
返回db.Question.OfType().OrderBy(x=>x.QuestionText).Where(x=>x.Active==true).ToList();应该在
OrderBy
之前的Where,想想看。他没有说if是一个可为空的字段。我应该像asawyer说的那样在OrderBy之前使用Where。谢谢你们两位。@asawyer:没问题。用你的建议更新了答案。谢谢你指出这一点out@RammtinAvar:没问题。也许你可以接受这个答案
db.Question
.OfType<CoreValueQuestion>()
.Where(a=>a.Active)
.OrderBy(x => x.QuestionText)
.ToList();