Wednesday, April 20, 2011

LINQ to Xml not equal to operator

I am using LINQ to XML. I want to use an equivalent of sql's <> operator in the where clause below....

var myBooks = from book in xDoc.Descendants("BOOKOB") 
              where book.Element("AUTHOR").Value

Please help!

From stackoverflow
  • Isn't != working?

  • You should be able to use != for not equal and == for equal to.

  • As others have said, you can use != perfectly easily - don't forget that even when you're using LINQ, you're writing C#, not SQL.

    You need to provide a value for it not to be equal to, of course, along with a select clause:

    var myBooks = from book in xDoc.Descendants("BOOKOB") 
                  where book.Element("AUTHOR").Value != "Jeff Atwood"
                  select book;
    

    For simple queries like this, I usually find "dot notation" simpler to read:

    var myBooks = xDoc.Descendants("BOOKOB")
                      .Where(b => b.Element("AUTHOR").Value != "Jeff Atwood");
    

0 comments:

Post a Comment