Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8040957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:03:52+00:00 2026-06-05T04:03:52+00:00

I am really confused to understand its internal working This is LINQ syntax string[]

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T04:03:53+00:00Added an answer on June 5, 2026 at 4:03 am

    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:

    string[] test=new test[]{"abc","","cd","","aa"};
    
    // this is the equivalent to your code
    // added explicit type to make it clearer, it's optional
    var a = from (string)x in test
            where !string.IsNullOrEmpty(x)
            select x;
    

    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 x variable was declared and it’s of type string. Why? because your array is an array of strings

    • The in test indicates the source IEnumerable<> to iterate – your array in this case

    • The where is pretty explanatory, it simply selects all not null strings from your array

    • And 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:

    x => !string.IsNullOrEmpty(x)
    

    You are creating an anonymous method and the method is being assigned to the delegate parameter. The x represents the string 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:

    // this is the equivalent to your code
    // added explicit type to make it clearer, it's optional
    var a = (from (string)x in test
            where !string.IsNullOrEmpty(x)
            select x).ToArray();
    

    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

    // this is basically the declaration of one overload of the Where method
    // the this in the parameter declaration, indicates this is an extension method which will be available to all IEnumerable<> objects
    // the Func<T, bool> is the interesting part, it is basically a delegate (as a reminder, the last parameter of the Func object indicates the type that must be returned, in this case is a bool)
    // the delegate is simply a pointer to a function 
    public IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate)
    {
    
       ...some logic
    
    
    
    
       ... yielding back the reuslts
       foreach(var r in res)
       {
          // here is the delegate in action
           if(predicate(r))
               yield return r;
       }
    }
    

    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 the x indicates the parameter passed back from the Where method to the external code, where you can inspect it and use it to filter your results

    • The 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 generic T which in this case is a string from your array

    Another way to declare the lambda expression is:

    test = test.Where(
                (string) x =>
                {
                    return !string.IsNullOrEmpty(x)
                })
               .ToArray();
    

    With this syntax is easy to show that they are actually methods (anonymous methods)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm really confused here. I am trying to understand this code (it's javascript) but
I'm really confused by a behaviour of LINQ I'm seeing and it's causing me
I'm really confused about the visitor pattern and its uses. I can't really seem
I'm really confused with an array im making, its gotten a bit confusing to
I'm really confused about this, tried to read a lot about diagrams but I
I am new to Magento and its EAV Model. I am very much confused
I really thought I was starting to understand how this works until I tried
I'm getting really confused over how to connect to MongoLab on Heroku. To connect
I'm really confused at the moment, and I hope you can help me find
I am really confused. I want to create a Select drop down menu using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.