Check for Existence of a Result in Linq-to-xml

Posted by NateD on Stack Overflow See other posts from Stack Overflow or by NateD
Published on 2010-03-29T18:15:13Z Indexed on 2010/03/29 18:23 UTC
Read the original article Hit count: 292

Filed under:
|

I'm using Linq-to-XML to do a simple "is this user registered" check (no security here, just making a list of registered users for a desktop app). How do I handle the result from a query like this:

var people = from person in currentDoc.Descendants("Users")
                     where (string)person.Element("User") == searchBox.Text
                     select person;

I understand the most common way to use the result would be something like

foreach (var line in people){
    //do something here
}

but what do you do if person comes back empty, which is what would happen if the person isn't registered?

I've looked around on this site and on MSDN and haven't found a really clear answer yet.

Extra credit: Give a good explanation of what people contains.

© Stack Overflow or respective owner

Related posts about c#

Related posts about linq-to-xml