Click here to Skip to main content
15,938,022 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an XML and it contains multiple sections. Many of which may or may not be repeated. I have grouped the sections and got a
List<IGrouping<string, XElement>>

Now, I want to find the index of a string in this list and print it, but every time I am running the loop, it is always 0.

Please help.


What I have tried:

//body contains the descendants of the main XML.    
List<IGrouping<string, XElement>> sectionlist = body.Descendants("sec").GroupBy(b => b.Element("title").Value).Select(b => b).ToList();

//Here, I want to find the index of uniquesections.Key in the list. "sectionlist"
             foreach (var uniquesections in sectionlist)
                 string uniquesectionskey = uniquesections.Key;
                 var indesxofstring = sectionlist.FindIndex(el => sectionlist.ElementAt(sectionkey).Key == uniquesectionskey);
Updated 5-Mar-19 7:34am

As OriginalGriff has indicated it is a bit difficult to try and figure out what you're trying to achieve. So I'll take a guess...

For this answer, I'm going to use an example to illustrate what I believe what you're trying to achieve using mock data.

1. We generate the index for the items before they are grouped
2. Group the indexed list
3. Search the group and flatten back to a single result holding the group key and the index of the item.
class Program
    static void Main()

        var actors = new List<Person>
            new Person() {Name = "Gilligan", Sex = "male"},
            new Person() {Name = "The Skipper", Sex = "male"},
            new Person() {Name = "Thurston Howell, III ", Sex = "male"},
            new Person() {Name = "Mrs. Howell", Sex = "female"},
            new Person() {Name = "Ginger Grant", Sex = "female"},
            new Person() {Name = "Professor", Sex = "male"},
            new Person() {Name = "Mary Ann Summers ", Sex = "female"},

        // search value
        var name1 = "Ginger Grant";

        // example 1 - flat list with index
        var indexedActors = actors.Select((x, i) => new {Index = i, Actor = x});

        // get the search result
        var result1 = indexedActors
            .FirstOrDefault(x => x.Actor.Name.Equals(name1));

        // get index of search value
        var index1 = result1?.Index ?? 0;

        // inxdex1 = 4

        // example 2 - grouped by sex list
        var groupedActors = indexedActors.GroupBy(x => x.Actor.Sex);

        var result2 = groupedActors
            .Select(x => new
                key = x.Key,
                value = x.FirstOrDefault(y => y.Actor.Name.Equals(name1))
            // only for valid group
            .Where(x => x.value != null);

        // get index & key of search value
        var index2 = result2?.Select(x => x.value.Index).FirstOrDefault() ?? 0;
        var key = result2?.Select(x => x.key).FirstOrDefault() ?? "";

        // inxdex2 = 4, key = "female"

class Person
    public string Name { get; set; }
    public string Sex { get; set; }

This answer is designed to return only the first result. You will need to modify if you require a list of results.
Share this answer
Maciej Los 3-Mar-19 10:56am    
We can't tell - it's going to depend on your input data far too much, and we don't have any access to that.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
Share this answer
Your question doesn't make a lot of sense. You're iterating through a list, and you want to find the index of the current item in the list? Just use a for loop instead of a foreach loop:
for (int index = 0; index < sectionlist.Count; index++)
    IGrouping<string, XElement> section = sectionlist[index];
    Console.WriteLine("The index of '{0}' is {1}.", section.Key, index);

C# for statement | Microsoft Docs[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900