I am really confused to understand its internal working
This is LINQ syntax
string[] test = new test[] { "abc", "", "cd", "", "aa" };
test = test.Where(x => !string.IsNullOrEmpty(x)).ToArray();
I am confused about where syntax how it manages. is it put all array in x? if yes then how it manage x null value?
or
if not then test array values put one by one in the x?
You should consider the rest of the answers, they are pretty accurate, what I want to show you and maybe this will help you to understand the syntax is that this kind of query can actually be represented in a query syntax like this:
if you are familiar with SQL, you will find this syntax easier to read, even when you do not know it, this syntax is cleaner.
When the code is compiled, this query syntax is automatically translated to the C# method syntax, in order to generate the IL, so if you dissasmbly a DLL you will see the method syntax instead of the query syntax
A brief explanation about this code:
As you can see an
xvariable was declared and it’s of typestring. Why? because your array is an array of stringsThe
in testindicates the sourceIEnumerable<>to iterate – your array in this caseThe
whereis pretty explanatory, it simply selects all not null strings from your arrayAnd finally the selects which actually is a projection of the data.
And all this is equivalent to your code
Now you might be asking yourself… When should I use one syntax or the other? Well they are equivalent but the query syntax operators are limited which means that most of the operations are done with method syntax instead of query syntax. What I always do is try to write the code easier to read some code is easier to understand if it is written with a query syntax.
About the method syntax, the
x => ...syntax is known as lambda expression, they might look weird if it is the first time you are working with them but you will love them eventually.Basically lambdas are shortcuts to delegates, so what you are doing with:
You are creating an anonymous method and the method is being assigned to the delegate parameter. The
xrepresents thestring variable.This topic is really extensive to try to explain it here, but I hope this has given you an idea of what’s behind.
Btw you can combine the syntax’s like this:
If you google LINQ is like googling porm lol the web is plagued with LINQ articles, samples, etc.
A good point of start would be the 101 samples from Microsoft
http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
EDIT
I will try to emulate the Where method so you can have a better example of the lambda expression
As you can see the delegate was called as a function (delegates are pointer to functions)
Butt what function??? the one you declared in your code
x => !string.IsNullOrEmpty(x)and thexindicates the parameter passed back from the Where method to the external code, where you can inspect it and use it to filter your resultsThe x => is an abbreviation to declare a delegate
!string.IsNullOrEmpty(x) this is the body of your anonymous method and as you can see it fulfills the requirements of the
Func<T, bool>it is returning a bool (the predicate to filter the elements of the array) and as a parameter, it received the genericTwhich in this case is a string from your arrayAnother way to declare the lambda expression is:
With this syntax is easy to show that they are actually methods (anonymous methods)