LINQ LambdaWhere() 未按预期进行过滤
我正在尝试使用 LINQ for EF 构建一个查询,以根据一些基本逻辑过滤结果。由于某种原因,即使执行以下Where()函数并设置正确的参数,也会返回所有数据,而不是Where()的过滤结果。
我已运行调试以确保我的 if() 语句确实允许Where() 在适当的时候运行,而且确实如此。
我缺少什么?
var dbReports = db.SubmitReports;
if (Referee != String.Empty)
dbReports.Where(u => (u.Refree == Referee || u.Ar1Official == Referee || u.Ar2Official == Referee || u.FourthOfficial == Referee));
if (TeamName != String.Empty)
dbReports.Where(u => (u.HomeTeam == TeamName || u.VisitingTeam == TeamName));
if (PlayedOnStart != DateTime.MinValue && PlayedOnEnd != DateTime.MinValue)
dbReports.Where(u => (u.PlayedOn >= PlayedOnStart && u.PlayedOn <= PlayedOnEnd));
if (StateAssociation != String.Empty)
dbReports.Where(u => (u.StateAssociation == StateAssociation || u.StateAssociation2 == StateAssociation));
if (Division != String.Empty)
dbReports.Where(u => u.Division == Division);
if (ProfessionalLeague != String.Empty)
dbReports.Where(u => u.ProfessionalLeague == ProfessionalLeague);
if (AgeGroup != String.Empty)
dbReports.Where(u => u.AgeGroup == AgeGroup);
return dbReports.ToList();
I'm trying to build a query using LINQ for EF to filter results based on some basic logic. For some reason, even with the following Where() functions being executed and setting the right parameters, all data is being returned instead of the filtered results from Where().
I have run debug to make sure that my if() statements are indeed allowing the Where() to run when appropriate, and it is.
What am I missing?
var dbReports = db.SubmitReports;
if (Referee != String.Empty)
dbReports.Where(u => (u.Refree == Referee || u.Ar1Official == Referee || u.Ar2Official == Referee || u.FourthOfficial == Referee));
if (TeamName != String.Empty)
dbReports.Where(u => (u.HomeTeam == TeamName || u.VisitingTeam == TeamName));
if (PlayedOnStart != DateTime.MinValue && PlayedOnEnd != DateTime.MinValue)
dbReports.Where(u => (u.PlayedOn >= PlayedOnStart && u.PlayedOn <= PlayedOnEnd));
if (StateAssociation != String.Empty)
dbReports.Where(u => (u.StateAssociation == StateAssociation || u.StateAssociation2 == StateAssociation));
if (Division != String.Empty)
dbReports.Where(u => u.Division == Division);
if (ProfessionalLeague != String.Empty)
dbReports.Where(u => u.ProfessionalLeague == ProfessionalLeague);
if (AgeGroup != String.Empty)
dbReports.Where(u => u.AgeGroup == AgeGroup);
return dbReports.ToList();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其中不修改现有查询 - 它创建新查询。您需要将调用结果分配给Where,否则结果将被丢弃。试试这个:
Where doesn't modify the existing query - it creates a new query. You need to assign the result of the call to Where to something otherwise the result is simply discarded. Try this:
您永远不会使用Where 方法的返回值。 Where 不修改它所应用的 IEnumerable,但返回一个 Linq 表达式,该表达式将在执行时(即调用 ToList 时)创建一个修改 IEnumerable。
You never use the return value of the Where method. Where does not modify the IEnumerable it is apply on but returns a Linq expression that will create a modify IEnumerable when executed (i.e when ToList is called).