Linq 查询语法和复杂的Where 条件

发布于 2024-12-02 15:56:08 字数 679 浏览 0 评论 0 原文

我想使用 Linq to Objects 查询根据相当复杂的选择标准从列表中选择某些成员(为了简化,此处表示为 return true:-)

使用Where 扩展方法,这工作得很好:

    var matches = (from wm in Members
                   select wm).Where(p =>
                   {
                       return true;
                   });

但是,当我尝试使用查询语法执行相同的操作时:

    var matches2 = (from wm in Members
                    where (p =>
                    {
                        return true;
                    })
                   select wm);

编译器对象

无法将 lambda 表达式转换为“bool”类型,因为它不是 委托类型

幕后发生了什么,如何在查询语法中使用 lamda 表达式?

I want to use a Linq to Objects query to select certain members from a list based on fairly complex selection criteria (for simplification represented here as return true :-)

Using the Where extension method, this works just fine:

    var matches = (from wm in Members
                   select wm).Where(p =>
                   {
                       return true;
                   });

However, when I attempt to do the same thing using the query syntax:

    var matches2 = (from wm in Members
                    where (p =>
                    {
                        return true;
                    })
                   select wm);

The compiler objects

Cannot convert lambda expression to type 'bool' because it is not
a delegate type

What is going on here behind the scenes, and how can I use a lamda expression with the query syntax?

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

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

发布评论

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

评论(1

一百个冬季 2024-12-09 15:56:08

您的查询被转换为如下内容:

Members.Where(member=> p=>{return true;});

...无法编译,因为(警告:可能存在技术错误) Where() 需要一个 Func 类型的参数。您给它一个参数,该参数将转换为类型 Func>。该错误告诉您 lambda 表达式无法转换为 bool,因为 lambda 表达式只能转换为委托类型(例如 Func<...>,而 bool 不是委托类型.(/结束笨拙的解释)

在查询语法中,您在 where 子句中输入的任何内容都位于生成的 Where( 的 lambda 表达式中的 => 之后) 方法调用。第一个片段可以重写为:

Members.Where(p=>true);

这相当于:

from m in members 
where true 
select m;

如果你确实需要一个带有语句块的 lambda,你可以这样做:

var lam = (Member m)=> {return true;};
from m in members
where lam(m)
select m;

或内联它:

from m in members
where ((Func<Member, bool>)(Member m)=> {return true;})(m)
select m;

或者你可以使用常规方法而不是匿名方法:

public bool DoStuff(int i)
{
    return true;
}

...

from m in members
where DoStuff(m)
select m;

your query gets translated to something like:

Members.Where(member=> p=>{return true;});

...which doesn't compile because (warning: technical errors probably be here) Where() expects a parameter of type Func<TSource, bool>. You are giving it a parameter that would get converted to type Func<TSource, Func<???, bool>>. The error is telling you that a lambda expression cannot be converted to a bool, because lambda expressions can only be converted to delegate types (e.g. Func<...>, and bool is not a delegate type.(/end bumbling explanation)

In query syntax, whatever you put in your where clause goes after the => in the lambda expression for the generated Where() method call. Note that your first snippet can be rewritten as:

Members.Where(p=>true);

which is equivalent to:

from m in members 
where true 
select m;

if you really need a lambda with a statement block in it you can do:

var lam = (Member m)=> {return true;};
from m in members
where lam(m)
select m;

or to inline it:

from m in members
where ((Func<Member, bool>)(Member m)=> {return true;})(m)
select m;

or you can use a regular method instead of an anonymous one:

public bool DoStuff(int i)
{
    return true;
}

...

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