如何在列表中找到值索引< igrouping< string,xelement>>? [英] How do I find index of value in list<igrouping<string, xelement>>?

查看:86
本文介绍了如何在列表中找到值索引< igrouping< string,xelement>>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML,它包含多个部分。其中许多可能会或可能不会重复。我已经对这些部分进行了分组,得到了一个

 List< IGrouping< string,XElement>> 

变量。



现在,我想在此列表中找到字符串的索引并打印出来,但每次运行循环时,它总是为0.



请帮助。



问候

Aman



我有什么试过:



  //   body包含主XML的后代。  
列表< IGrouping< string,XElement>> sectionlist = body.Descendants( sec)。GroupBy(b = > b.Element( title)。值)。选择(b = > b).ToList();

// 在这里,我想在列表中找到uniquesections.Key的索引。 sectionlist
foreach var uniquesections in sectionlist)
{
string uniquesectionskey = uniquesections.Key;
var indesxofstring = sectionlist.FindIndex(el = > sectionlist.ElementAt(sectionkey)。 Key == uniquesectionskey);
sectionkey ++;
}

解决方案

正如OriginalGriff所说,尝试找出你想要实现的目标有点困难。所以我会猜测......



对于这个答案,我将用一个例子来说明我相信你想要实现的目标使用模拟数据。



1.我们在项目分组之前生成索引

2.对索引列表进行分组

3.搜索该组并展平回一个包含组密钥和项目索引的结果。

  class 计划
{
静态 void Main ()
{

var actors = new 列表< Person> ;
{
new Person(){Name = Gilligan,性别= 男性},
new Person(){Name = The Skipper ,性别= 男性},
new Person(){Name = Thurston Howell,III,性别= 男性},
new Person(){Name = 豪威尔夫人,性别= female},
new Person(){Name = Ginger Grant,性别= < span class =code-string> female},
new Person(){Name = 教授,性别= male},
new Person(){Name = < span class =code-string> Mary Ann Summers,性别= female},
};

// 搜索值
var name1 = Ginger Grant;

// 示例1 - 带索引的平面列表
< span class =code-keyword> var indexedActors = actors.Select((x,i)= > new {Index = i,Actor = x});

// 获取搜索结果
var result1 = indexedActors
.FirstOrDefault(x = > x.Actor.Name.Equals(name1)) ;

// 获取搜索值索引
var index1 = result1?.Index ?? 0 ;

// inxdex1 = 4


// 示例2 - 按性别分组列出
var groupedActors = indexedActors.GroupBy(x = > x.Actor.Sex);

var result2 = groupedActors
。选择(x = > new
{
key = x.Key,
value = x.FirstOrDefault(y = > y.Actor.Name.Equals(name1))
})
// 仅适用于有效群组
.Where(x = > x。 value != null );

// get index&搜索值的关键字
var index2 = result2?.Select(x = > x。 value .Index).FirstOrDefault()?? 0 ;
var key = result2?.Select(x = > x.key).FirstOrDefault ()?? ;

// inxdex2 = 4,key =female
}
}

class
{
public string 名称{ get ; set ; }
public string 性别{ get ; set ; }
}



此答案旨在仅返回第一个结果。如果需要结果列表,则需要修改。


我们无法分辨 - 它将依赖于您的输入数据太多,我们没有任何访问权限对此。

所以,这将取决于你。

幸运的是,你有一个工具可以帮助你找到正在发生的事情:调试器。

在函数的第一行放置一个断点,然后通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但是我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!


你的问题没有太多帮助感。您正在遍历列表,并且想要查找列表中当前项的索引?只需使用代替循环而不是 foreach 循环:

  for  int  index =  0 ; index <  sectionlist.Count; index ++)
{
IGrouping< string,XElement> section = sectionlist [index];
Console.WriteLine( {0}的索引为{1}。,section.Key,index);
...
}



C#for statement | Microsoft Docs [ ^ ]


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>>

variable.

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.

Regards
Aman

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);
                 sectionkey++;
             }

解决方案

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.


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!


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[^]


这篇关于如何在列表中找到值索引&lt; igrouping&lt; string,xelement&gt;&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆