在具有多个Where语句的循环中过滤IQueryable

发布于 2024-11-09 08:01:48 字数 996 浏览 0 评论 0原文

我有这段代码,

private static IQueryable<Persoon> Filter(IQueryable<Persoon> qF, IDictionary<string, string> filter)
{
    IQueryable<Persoon> temp;
    temp = qF;
    foreach (var key in filter)
    {
        if (key.Key == "naam")
        {
            temp = temp.Where(f => f.Naam == key.Value);
        }
        else if (key.Key == "leeftijd")
        {
            temp = temp.Where(af => af.Leeftijd != null && af.Leeftijd.AantalJaarOud.ToString() == key.Value);
        }
    }
    return temp;

}

它的作用(它是一个简化版本,用于测试行为)是为这个函数提供一个 IQueryable 的 Persoon (来自数据库)和一个过滤器列表。

所以你给过滤器 naam,john 和 leeftijd,30 你得到所有名为 John 和 Age 30 的 Persoon objecten。

当我首先进入循环时,紧接着我在后面的 } 处执行第一个 where (leeftijd where),我看到该 tmp 有 3 个对象。然后代码第二次进入循环,进入第一个 If (其中过滤器 eq naam),就在那里,当我查看 tmp 时,它只有 0 个对象。

第一个认为它不起作用的事实是,该函数没有返回结果(应该是 2:3 个 30 和 2 个约翰)。所以我得出结论,多重。问题出在哪里。

但现在我发现即使在我执行第二个地方之前,温度也是空的。

我做错了什么?

i have this code

private static IQueryable<Persoon> Filter(IQueryable<Persoon> qF, IDictionary<string, string> filter)
{
    IQueryable<Persoon> temp;
    temp = qF;
    foreach (var key in filter)
    {
        if (key.Key == "naam")
        {
            temp = temp.Where(f => f.Naam == key.Value);
        }
        else if (key.Key == "leeftijd")
        {
            temp = temp.Where(af => af.Leeftijd != null && af.Leeftijd.AantalJaarOud.ToString() == key.Value);
        }
    }
    return temp;

}

what it does (it's a simplified version which is made to test the behaviour) is that you give this function a IQueryable of Persoon (from the database) and a list of filters.

So you give the filter naam,john and leefttijd,30 you get all Persoon objecten named John and Age 30.

When i enter the loop first, right after i do the first where (the leeftijd where) at the } after it, i see that tmp has 3 objects. Then the code goes for the second time in the loop, enters the first If (where filter eq naam) and right there, when i look at tmp, it only has 0 objects.

what the first view of it not working was, was the fact that the function returns no results (should be 2: 3 30's and 2 Johns of those). So i concluded that the multiple .Where was the problem.

But now i see that the temp is empty even BEFORE i do the second where.

What am i doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

偷得浮生 2024-11-16 08:01:48

LINQ 的 lambda 表达式使用后期参数绑定,因此当最终处理表达式时,变量“key”不再指向正确的值。

尝试更改代码以将密钥存储在局部变量中并使用它:

private static IQueryable<Persoon> Filter(IQueryable<Persoon> qF, IDictionary<string, string> filter)
{
    IQueryable<Persoon> temp;
    temp = qF;
    foreach (var key in filter)
    {
        var currentKeyValue = key.Value;
        if (key.Key == "naam")
        {
            temp = temp.Where(f => f.Naam == currentKeyValue);
        }
        else if (key.Key == "leeftijd")
        {
            temp = temp.Where(af => af.Leeftijd != null && af.Leeftijd.AantalJaarOud == Int32.Parse(currentKeyValue));
        }
    }
    return temp;

}

我认为您应该更改的另一件事是将年龄字段转换为字符串而不是相反的方向。因此数据库正在比较数字而不是字符串。

LINQ's lambda expressions use late parameter binding so when the xpression is finally processed the variable "key" no longer points at the right values.

Try changing your code to store key in a local variable and use it instead:

private static IQueryable<Persoon> Filter(IQueryable<Persoon> qF, IDictionary<string, string> filter)
{
    IQueryable<Persoon> temp;
    temp = qF;
    foreach (var key in filter)
    {
        var currentKeyValue = key.Value;
        if (key.Key == "naam")
        {
            temp = temp.Where(f => f.Naam == currentKeyValue);
        }
        else if (key.Key == "leeftijd")
        {
            temp = temp.Where(af => af.Leeftijd != null && af.Leeftijd.AantalJaarOud == Int32.Parse(currentKeyValue));
        }
    }
    return temp;

}

Another thing I think you should change is the casting of the age field to string rather than the opposite direction. therefore the database is comparing numbers rather than strings.

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