简体   繁体   English

为什么不能分配LINQ Find()或FirstOrDefault()结果的返回值?

[英]Why can't I assign the return of a LINQ Find() or FirstOrDefault() result?

class Program
{
    static void Main(string[] args)
    {
        var people = new List<Person>
        {
            new Person { Name = "foo" },
            new Person { Name = "bar" }
        };

        // ERROR: Left-hand side must be a variable, property, or indexer
        people.FirstOrDefault(x => x.Name == "foo") = new Person();
    }
}

public class Person
{
    public string Name { get; set; }
}

Why is the above FirstOrDefault line not valid .NET code? 为什么上面的FirstOrDefault行无效的.NET代码? If the return of FirstOrDefault() isn't a variable, what is it? 如果FirstOrDefault()的返回值不是变量,那是什么?

Edit: I understand how to make this valid .NET code - my point is trying to get a concrete answer as to why this is not valid. 编辑:我知道如何使此有效的.NET代码-我的观点是试图就为什么这无效是一个具体的答案。 A definition from the docs perhaps as to why. 来自文档的定义也许是关于原因的。 I don't need to see how to make it correct :) 我不需要看如何使其正确:)

you cannot assign to the return of a function you need to store it in a variable 您无法分配给需要将其存储在变量中的函数的返回值

var person = people.FirstOrDefault(x => x.Name == "foo") ?? new Person();

FirstOrDefault is a Get method it is not an array indexer you cannot use it to set values, For instance what do you expect your original statement accomplish FirstOrDefault是一个Get方法,它不是数组索引器,不能使用它设置值,例如,您希望原始语句完成什么操作

 people.FirstOrDefault(x => x.Name == "foo") = new Person();

you cannot use it for anything afterwards until it is in a variable. 除非它在变量中,否则以后不能将其用于任何东西。

A method returns used for returning values if you want to set in the method you must use an out parameter or reference, or a property. 如果要在方法中进行设置,则方法返回用于返回值,必须使用out参数或引用或属性。

Why is the above FirstOrDefault line not valid .NET code? 为什么上面的FirstOrDefault行无效的.NET代码?

Because you can only assign a value to a variable, and FirstOrDefault doesn't return a variable. 因为您只能将值分配给变量,并且FirstOrDefault不会返回变量。

If the return of FirstOrDefault() isn't a variable, what is it? 如果FirstOrDefault()的返回值不是变量,那是什么?

It's a value. 这是一个价值。 You can store the result of FirstOrDefault in a variable, bit it isn't itself a variable 您可以将FirstOrDefault的结果存储在变量中,但它本身并不是变量

If you want to assign to the first element of the list, you should just access it at the index: people[0] = new Person() . 如果要分配给列表的第一个元素,则应在索引处访问它: people[0] = new Person()

To answer your other question, it's not a variable; 要回答您的另一个问题,它不是一个变量。 it's an object. 这是一个对象。 Imagine I tried to write "foo" = "bar" . 想象一下我试图写"foo" = "bar" That would throw an exception -- what does it mean to assign a string literal to another? 那将引发异常-将字符串文字分配给另一个意味着什么? Your code is logically equivalent. 您的代码在逻辑上是等效的。

In C# (currently), you cannot set a value to a function call. 在C#中(当前),您不能为函数调用设置值。 Instead, you should say: 相反,您应该说:

var person = people.FirstOrDefault(x => x.Name == "foo");

if (person != null) {
    person = new Person();
}

The C# spec says that the = operator: C#规范说=运算符:

stores the value of its right-hand operand in the storage location, property, or indexer denoted by its left-hand operand and returns the value as its result ( https://msdn.microsoft.com/en-us/library/sbkb459w.aspx ) 将其右侧操作数的值存储在其左侧操作数表示的存储位置,属性或索引器中,并返回该值作为其结果( https://msdn.microsoft.com/zh-cn/library/sbkb459w .aspx

A function call is not a storage location, property, or indexer. 函数调用不是存储位置,属性或索引器。

This may sound confusing, but think about it this way: 这听起来可能令人困惑,但是请这样考虑:

The = operator is assignment. =运算符是赋值。 x = y means that, when you ask for x , give me y (remember basic algebra?). x = y表示当您要求x ,给我y (还记得基本代数吗?)。

You cannot say, "for this function, I want you to always give me y " ( FirstOrDefault(x => x.foo) = y ). 您不能说“对于此功能,我希望您始终给我y ”( FirstOrDefault(x => x.foo) = y )。 That kinda goes against a function, which returns a result based on its definition! 那有点违背一个函数,该函数根据其定义返回结果! :) :)

FirstOrDefault() is a method call and it cannot be used as a variable. FirstOrDefault()是一个方法调用,不能用作变量。

Answer to your question: the value of right hand side is assigned to left hand side, but an object cannot be on the left hand side; 您的问题的答案:右侧的值分配给左侧,但是对象不能位于左侧; only a variable can. 只有一个变量可以。

You are trying to assign result of FirstOrDefault() method to People class constructor. 您试图将FirstOrDefault()方法的结果分配给People类构造函数。

I assume you're trying to retrieve person "foo" to do something with it later. 我假设您正在尝试检索人“ foo”,以便稍后对其进行处理。 You need to store it in a variable. 您需要将其存储在变量中。 Your code is not valid C# syntax. 您的代码不是有效的C#语法。

class Program
{
    static void Main(string[] args)
    {
        var people = new List<Person>
        {
            new Person { Name = "foo" },
            new Person { Name = "bar" }
        };

        var fooPerson = people.FirstOrDefault(x => x.Name == "foo");

        // Use fooPerson
    }
}

in the code 在代码中

people.FirstOrDefault(x => x.Name == "foo") = new Person();

we see an assignment statement. 我们看到一个分配声明。 Assigning one value to another. 将一个值分配给另一个。 Often with C style languages we call this assigning an lvalue (for left side) an rvalue (or right side.) 通常,对于C风格的语言,我们称之为分配左值(左侧)或右值(或右侧)。

lvalue = rvalue.

There are limits on what the lvalue and rvalue can be. 左值和右值可以有限制。 The rvalue must be able to be evaluated to the same type as the lvalue and the lvalue must be a location in memory (sometimes called a variable.) 右值必须能够被评估为与左值相同的类型,并且左值必须是内存中的位置(有时称为变量)。

If you want to find out more about these issues you can look at language references and how they talk about the lvalue and rvalue in assignment statements. 如果要查找有关这些问题的更多信息,可以查看语言参考以及它们如何谈论赋值语句中的左值和右值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM