locked
check if linq conditions has results RRS feed

  • Question

  • User-317791795 posted

    Hi,

    I am making some validations in an xml and remove certain elements based on date fields the below LINQ will do the job just fine.

    XDocument newXML = XDocument.Load(new StringReader(xmlValue));
    var q = from node in newXML.Descendants("Date") let attr = node.Value
            where attr != null && DateTime.ParseExact (attr, "dd-MM-yyyy", CultureInfo.InvariantCulture)
            < DateTime.Today select node.Parent;
    q.ToList().ForEach(x => x.Remove());

    How can I check whether this linq has identified and removed the items?

         OR

    How can I load the results of newXML as an xml document?

    Monday, October 31, 2016 6:37 AM

Answers

  • User303363814 posted

    newXML is an XDocument and your code never changes it at any point.  Notice how newXML is only on the left hand side of the first assignment statement.

    q is a totally separate variable.  Think of it like

    int x = 3;
    int y = x -1;

    This does not change x.  In the same way, creating a variable 'q' which uses another variable 'newXML' as its starting point does not change the starting point.

    In the last statement a new temporary variable is created by the ToList() method and then for each element of that newly created temporary variable you do something and do not save the results.

    So ...

    How can I check whether this linq has identified and removed the items?
    Removed from what?  You never ask for newXML to be changed.  You never ask for q to be changed.  Some anonymous variable is created and then discarded.  (In general, if you want to check the value of a variable then use the debugger to inspect it or write its value to some output stream)

    How can I load the results of newXML as an xml document?
    newXML is already an xml document.  What do you mean by this question?  What do you mean by 'the results of newXML'?  newXML is a variable of type XDocument - there is no property called 'results'.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 31, 2016 6:54 AM

All replies

  • User303363814 posted

    newXML is an XDocument and your code never changes it at any point.  Notice how newXML is only on the left hand side of the first assignment statement.

    q is a totally separate variable.  Think of it like

    int x = 3;
    int y = x -1;

    This does not change x.  In the same way, creating a variable 'q' which uses another variable 'newXML' as its starting point does not change the starting point.

    In the last statement a new temporary variable is created by the ToList() method and then for each element of that newly created temporary variable you do something and do not save the results.

    So ...

    How can I check whether this linq has identified and removed the items?
    Removed from what?  You never ask for newXML to be changed.  You never ask for q to be changed.  Some anonymous variable is created and then discarded.  (In general, if you want to check the value of a variable then use the debugger to inspect it or write its value to some output stream)

    How can I load the results of newXML as an xml document?
    newXML is already an xml document.  What do you mean by this question?  What do you mean by 'the results of newXML'?  newXML is a variable of type XDocument - there is no property called 'results'.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 31, 2016 6:54 AM
  • User283571144 posted

    Hi vickyt5,

    How can I check whether this linq has identified and removed the items?

    According to your description, I suggest you could use if condition check the q has value.

    If q has value means you will remove its parent nodes.

    Then you could use foreach method to loop the q's list and you could use a parameter to store this value to know how many nodes has deleted.

    More details, you could refer to follow codes:

    Xml:

    <?xml version="1.0" encoding="utf-8"?>
    <Dates>
      <Department>
        <ID>Food</ID>
        <ExpiryDate>25-11-2016</ExpiryDate>
      </Department>
      <Department>
        <ID>Sport</ID>
        <ExpiryDate>26-10-2016</ExpiryDate>
      </Department>
    </Dates>

    Code:

      XDocument loadedData = XDocument.Load(@"C:\Users\v-wenzz\documents\visual studio 2015\Projects\Error\ConsoleApplication1\XMLFile1.xml");
                var q = from node in loadedData.Descendants("ExpiryDate")
                        let attr = node.Value
                        where attr != null && DateTime.ParseExact(attr, "dd-MM-yyyy", CultureInfo.InvariantCulture) < DateTime.Today
                        select node.Parent;
                int i = 0;
                string result = null;
                if (q.ToList() != null)
                {
                     result = "Q has value";
                    foreach (var item in q.ToList())
                    {
                        item.Remove();
                        i++;
                    }
                }
                else
                {
                     result = "Q has no value";
                }
                result = result + " remove " + i + " Nodes ";
    
                int s = 0;

    Result:

    How can I load the results of newXML as an xml document?

    I couldn't understand your requirement.

    Could you please explain more about "load the results of newXML as an xml document"?

    In my opinion, after you delete the nodes, newXML is already a new xml document.

    Best Regards,

    Brando

    Tuesday, November 1, 2016 8:45 AM