I have a list of clients for which I just want to return the name of the client (appended to the first and last name). Can be easily done with the following Linq:
return customers.Select(a => string.Format("{0} {1}", a.Forename, a.Surname).First();
If I understood things correctly, then "Select" will be executed only for lines required by the operator. Thus, for the above code, instead of highlighting the structure of the list of lines and only using the first one (discarding the rest), the select statement only works for the first line (.First () is required) and then completes, so it only builds the line for one line ( the first is back). It is right?
Obviously placing ".ToList ()" between .Select () and .First will cause it to build a list of strings and then take only the first, but is there any other way that the aforementioned Linq could end up doing too much work? This is what I feel that I have to be sure of the answer, but it is best to confirm this.
source
share