LINQ LambdaWhere() 未按预期进行过滤

发布于 2024-10-08 12:14:03 字数 1171 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲歌长辞 2024-10-15 12:14:03

其中不修改现有查询 - 它创建查询。您需要将调用结果分配给Where,否则结果将被丢弃。试试这个:

IQueryable<Report> dbReports = db.SubmitReports;

if (...)
{
    dbReports = dbReports.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:

IQueryable<Report> dbReports = db.SubmitReports;

if (...)
{
    dbReports = dbReports.Where(...);
}
锦爱 2024-10-15 12:14:03

您永远不会使用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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文