谓词里面的谓词<>查询未命中? [英] Predicate inside a Where<> query not hit?

查看:76
本文介绍了谓词里面的谓词<>查询未命中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要为列表中的每个项目调用方法。所以,我使用了Where<>查询如下,

I need to call the method for each item in the list. So, I have used Where<> query as follows,

            List<string> list = new List<string>();
            list.Add("Name1");
            list.Add("Name2");
            list.Add("Name3");

            var name = list.Where(n =>
            {
                return CheckName(n);
            }) != null;

但在上述情况下,CheckName()未命中。如果我使用FirstOrDefault<>,则会触发相同的方法。我不知道这是一个框架中断还是我走错了。

But in the above case, CheckName() not hit. The same method is triggered if I use FirstOrDefault<>. I don't know whether it is a framework break or I am going in a wrong way.

作为附加信息,我使用.NET  Framework 4.5。

As additional info, I am using.NET Framework 4.5.

有没有人遇到此错误?如果有,是否有任何解决方案可以克服这个问题?

Has anyone experienced this error? If so, is there any solution to overcome this issue?

问候,

Divakar。

推荐答案

Where子句适用于我,使用一个小的测试CheckName()方法定义为:

Well the Where clause works for me with a little test CheckName() method defined as:

public bool CheckName(string n)
{
    return n == "Name2";
} 




即。所以只有一个名字实际上返回true。

i.e. so only one of the names actually returns true.

但是你正在检查Where调用的结果!= null。

But you are checking the result of the Where call against !=null.

 var name = list.Where(n =>
{
        return CheckName(n);
}) != null;

Where子句将始终返回一个列表,即使CheckName每次都返回false并且列表为空。

The Where clause will always return a list, even if CheckName returns false every time and the list is empty.

所以它始终是!= null并且您的名称变量将始终为真。

So it will always be !=null and your name variable will always be true.


这篇关于谓词里面的谓词&lt;&gt;查询未命中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆