+ 1

Variable in LINQ query. What is defined in "from" and what is defined in "select"

for example var userQuery = from user in users select user; When you use linq, the variable "user" is used twice. What is the purpose of the variable "user" next to from. What is the purpose of the variable "user" after select. Do these two variables complement each other ?

2nd Apr 2021, 6:40 PM
sneeze
sneeze - avatar
2 Respostas
+ 2
I think the SQL like linq syntax is kinda weird, I prefer not using it. In "proper" C# this is of course just users.Select(user => user) But perhaps the way it's written in your example is easier to understand as some javascript-like foreach loop. Pseudocode: foreach (user in users) { return user; } Or perhaps for (user in users) { select user; } That is to say that the first `user` is the variable into which you are assigning as you are stepping through the list, and the second `user` is what you wish to return. A slightly less trivial example being from user in users select user.name;
3rd Apr 2021, 12:28 AM
Schindlabua
Schindlabua - avatar