LINQ to XML - How to tell if the current element is the first element

I have the following XML where I am extracting all entry elements and looping over them as follows:

How do I check if entry is the first entry element in entries?

var entries = (from entry in doc.Elements("entry")
                           select entry);

foreach (var entry in entries)
{
    //...some code...
    //check if entry is the first entry element in entries 
        //(thought of using an extension method but i'm not sure how to do the comparison)

}

Open in new window


<log>
   <entry timestamp="2015043014444383" actor="1234" authenticatedactor="1234" session="" effective="20150401" caller="Job.CopyJob" action="Create" type="create" s="HCM" id="014cfdb3-1d16-0000-0000-a23b4e08dd31">
      <field name="HROrganization" t="0" s="4" d="0">
         <nv><![CDATA[ABC]]></nv>
      </field>
      <field name="Position" t="1" s="9" d="0">
         <nv><![CDATA[442]]></nv>
      </field>
      <field name="ShortDescription" t="30" s="20" d="0">
         <nv><![CDATA[210007A]]></nv>
      </field>
      <field name="Description" t="30" s="60" d="0">
         <nv><![CDATA[Security Coordinator]]></nv>
      </field>
      <field name="Active" t="41">
         <nv><![CDATA[true]]></nv>
      </field>
      <field name="Summary" t="38">
         <nv />
      </field>
      <field name="Responsibilities" t="38">
         <nv />
      </field>
      <field name="OtherInformation" t="38">
         <nv />
      </field>
      <field name="PositionDescriptionDate" t="5">
         <nv><![CDATA[00000000]]></nv>
      </field>
      <field name="HROrganizationUnit" t="1" s="6" d="0">
         <nv><![CDATA[0]]></nv>
      </field>
      <field name="Job" t="1" s="9" d="0">
         <nv><![CDATA[442]]></nv>
      </field>
      <field name="Location" t="0" s="20" d="0">
         <nv />
      </field>
      <field name="PositionLevel" t="0" s="20" d="0">
         <nv />
      </field>
      <field name="PositionFamily" t="0" s="20" d="0">
         <nv />
      </field>
      <field name="PositionCategory" t="0" s="20" d="0">
         <nv />
      </field>
      <field name="PositionSubCategory" t="0" s="20" d="0">
         <nv />
      </field>
      <field name="WorkSchedule" t="0" s="20" d="0">
         <nv />
      </field>
      <field name="Shift" t="1" s="1" d="0">
         <nv><![CDATA[0]]></nv>
      </field>
      <field name="AnnualHoursPer1FTE" t="1" s="4" d="0">
         <nv><![CDATA[0]]></nv>
      </field>
      <field name="DirectSupervisor" t="1" s="9" d="0">
         <nv><![CDATA[0]]></nv>
      </field>
      <field name="IndirectSupervisor" t="1" s="9" d="0">
         <nv><![CDATA[0]]></nv>
      </field>
      <field name="PositionIsSupervisor" t="1" s="9" d="0">
         <nv><![CDATA[0]]></nv>
      </field>
      <field name="SynchronizedToSupervisor" t="41">
         <nv><![CDATA[false]]></nv>
      </field>
      <field name="BargainingUnit" t="0" s="20" d="0">
         <nv />
      </field>
      <field name="Union" t="0" s="20" d="0">
         <nv />
      </field>
      <field name="PayRateType" t="0" s="1" d="0">
         <nv />
      </field>
      <field name="PayRateCurrency" t="0" s="5" d="0">
         <nv />
      </field>
      <field name="PayRate" t="2" s="18" d="6">
         <nv><![CDATA[0]]></nv>
      </field>
      <field name="PayFrequency" t="1" s="1" d="0">
         <nv><![CDATA[0]]></nv>
      </field>
      <field name="FTE" t="2" s="7" d="6">
         <nv><![CDATA[0]]></nv>
      </field>
      <field name="CostCenter" t="0" s="30" d="0">
         <nv />
      </field>
      <field name="PaymentSchedule" t="0" s="20" d="0">
         <nv />
      </field>
      <field name="NumberOfMonths" t="1" s="2" d="0">
         <nv><![CDATA[0]]></nv>
      </field>
      <field name="SalaryStructure" t="0" s="20" d="0">
         <nv><![CDATA[NE]]></nv>
      </field>
      <field name="SalaryStructureGrade" t="1" s="3" d="0">
         <nv><![CDATA[17]]></nv>
      </field>
      <field name="GeographicDifferentialCode" t="0" s="20" d="0">
         <nv />
      </field>
      <field name="StepAndGradeSchedule" t="0" s="20" d="0">
         <nv />
      </field>
      <field name="PayGrade" t="1" s="4" d="0">
         <nv><![CDATA[0]]></nv>
      </field>
      <field name="PayStep" t="1" s="6" d="0">
         <nv><![CDATA[0]]></nv>
      </field>
      <field name="KeyPosition" t="41">
         <nv><![CDATA[false]]></nv>
      </field>
      <field name="KeyPositionReason" t="0" s="20" d="0">
         <nv />
      </field>
      <field name="CriticalPosition" t="41">
         <nv><![CDATA[false]]></nv>
      </field>
      <field name="CriticalPositionReason" t="0" s="20" d="0">
         <nv />
      </field>
      <field name="BenchStrength" t="1" s="2" d="0">
         <nv><![CDATA[0]]></nv>
      </field>
   </entry>
   <entry timestamp="2015043014471892" actor="1234" authenticatedactor="1234" session="" effective="20150401" caller="Job.Update" action="UpdateFromJob" type="update" s="HCM" id="014cfdb3-1d16-0000-0000-a2d24e08dd31">
      <field name="SalaryStructure" t="0" s="20" d="0">
         <ov><![CDATA[NE]]></ov>
         <nv><![CDATA[EXEMPT]]></nv>
      </field>
   </entry>
   <entry timestamp="2015043015052337" actor="1234" authenticatedactor="1234" session="" effective="20150401" caller="Position.Update" action="Update" type="update" s="HCM" id="014cfdb3-1d16-0000-0000-a61f4e08dd31">
      <field name="HROrganizationUnit" t="1" s="6" d="0">
         <ov><![CDATA[0]]></ov>
         <nv><![CDATA[18]]></nv>
      </field>
      <field name="Location" t="0" s="20" d="0">
         <ov />
         <nv><![CDATA[xyz]]></nv>
      </field>
      <field name="PositionLevel" t="0" s="20" d="0">
         <ov />
         <nv><![CDATA[1]]></nv>
      </field>
      <field name="PositionFamily" t="0" s="20" d="0">
         <ov />
         <nv><![CDATA[PROFESSIONAL]]></nv>
      </field>
      <field name="DirectSupervisor" t="1" s="9" d="0">
         <ov><![CDATA[0]]></ov>
         <nv><![CDATA[42]]></nv>
      </field>
      <field name="CostCenter" t="0" s="30" d="0">
         <ov />
         <nv><![CDATA[210007]]></nv>
      </field>
   </entry>
   <entry timestamp="2015043015060462" actor="1234" authenticatedactor="1234" session="" effective="20150401" caller="Position.Update" action="Update" type="update" s="HCM" id="014cfdb3-1d16-0000-0000-a6984e08dd31">
      <field name="AnnualHoursPer1FTE" t="1" s="4" d="0">
         <ov><![CDATA[0]]></ov>
         <nv><![CDATA[2080]]></nv>
      </field>
      <field name="PayRateType" t="0" s="1" d="0">
         <ov />
         <nv><![CDATA[2]]></nv>
      </field>
      <field name="PayFrequency" t="1" s="1" d="0">
         <ov><![CDATA[0]]></ov>
         <nv><![CDATA[2]]></nv>
      </field>
      <field name="FTE" t="2" s="7" d="6">
         <ov><![CDATA[0.000000]]></ov>
         <nv><![CDATA[1.000000]]></nv>
      </field>
      <field name="PaymentSchedule" t="0" s="20" d="0">
         <ov />
         <nv><![CDATA[M-_]]></nv>
      </field>
      <field name="GeographicDifferentialCode" t="0" s="20" d="0">
         <ov />
         <nv><![CDATA[ZONE NYC]]></nv>
      </field>
   </entry>
   <entry timestamp="2015043018142456" actor="1234" authenticatedactor="1234" session="" caller="Position.Update" action="Update" type="update" s="HCM" id="014cfdb3-1d16-0000-0000-c0494e08dd31">
      <field name="DirectSupervisor" t="1" s="9" d="0">
         <ov><![CDATA[42]]></ov>
         <nv><![CDATA[159]]></nv>=
      </field>
   </entry>
</log>

Open in new window

nightshadzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi nightshadz;

When you use Linq to XML the results of the query is always in document order. Therefore when returning all entry nodes the first in the list is the first from the document. This line of code from code snippet, XElement firstEntry = entries.FirstOrDefault( );, is your first entry node from the XML document. In the code snippet using the Equals method uses reference equals to find the same node when iterating through the nodes, but by definition will be the first item through the iteration.

XElement doc = XElement.Load("C:/.../...xml");

var entries = (from entry in doc.Elements("entry")
               select entry);
               
XElement firstEntry = entries.FirstOrDefault( );

foreach (var entry in entries)
{
    //...some code...
    if( firstEntry.Equals(entry) )
    {
        Console.WriteLine("They are equal");
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nightshadzAuthor Commented:
Thanks! I was using FirstOrDefault, but I was doing as...

entries.Descendants("entry").FirstOrDefault()
entries.Elements("entry").FirstOrDefault()

... and would always get null back and didn't know what was going on.

I turned your suggestion into an extension method so now I can just call it as.

bool first = entry.IsFirstEntry(entries);

        public static bool IsFirstEntry(this XElement entry, IEnumerable<XElement> entries)
        {
            return entries.FirstOrDefault().Equals(entry) ? true : false;
        }

Open in new window

0
Fernando SotoRetiredCommented:
Very good. Have a great day.
0
nightshadzAuthor Commented:
You too, Fernando. Thanks again for all your help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.