Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:在列表中查找类字段_C#_List_Generic Collections - Fatal编程技术网

C#:在列表中查找类字段

C#:在列表中查找类字段,c#,list,generic-collections,C#,List,Generic Collections,我有以下代码: class exampleClass { int var1; int var2; string var3 } List<exampleClass> myList = new List<exampleClass>() myList.Add(new exampleClass { var1 = 10, var2 = 100, var3 = "a"}); myList.Add(new exampleClass { var1 = 20,

我有以下代码:

class exampleClass
{
    int var1;
    int var2;
    string var3
}

List<exampleClass> myList = new List<exampleClass>()

myList.Add(new exampleClass { var1 = 10, var2 = 100, var3 = "a"});
myList.Add(new exampleClass { var1 = 20, var2 = 200, var3 = "b"});
etc.
类示例类
{
int-var1;
int-var2;
字符串变量3
}
列表myList=新列表()
Add(新示例类{var1=10,var2=100,var3=“a”});
Add(新示例类{var1=20,var2=200,var3=“b”});
等
如果我想在var1=20的列表中找到对象的索引#,我该怎么做(在本例中,var1=20在myList[1]中)

我尝试使用Contain(),但出现了错误


在一个相关的主题上,假设var1的每个值都是唯一的,如果我使用一个以var1为键的字典,它会更快吗。谢谢。

这里有一些关于如何做到这一点的建议。根据您的需要,它提供了多种解决方案。

以下是一个解决方案。它根据您的需要提供多种解决方案。

更快是一个非常相对的术语。它始终取决于集合中对象的数量。不过,这可能会更容易,也更有意义

以您的例子:

class exampleClass
{
    int var1;
    int var2;
    string var3
}

List<exampleClass> myList = new List<exampleClass>()

myList.Add(new exampleClass { var1 = 10, var2 = 100, var3 = "a"});
myList.Add(new exampleClass { var1 = 20, var2 = 200, var3 = "b"});

var dict = myList.ToDictionary(k => k.var1);
exampleClass item1 = dict[20];
类示例类
{
int-var1;
int-var2;
字符串变量3
}
列表myList=新列表()
Add(新示例类{var1=10,var2=100,var3=“a”});
Add(新示例类{var1=20,var2=200,var3=“b”});
var dict=myList.ToDictionary(k=>k.var1);
exampleClass item1=dict[20];

更快是一个非常相对的术语。它始终取决于集合中对象的数量。不过,这可能会更容易,也更有意义

以您的例子:

class exampleClass
{
    int var1;
    int var2;
    string var3
}

List<exampleClass> myList = new List<exampleClass>()

myList.Add(new exampleClass { var1 = 10, var2 = 100, var3 = "a"});
myList.Add(new exampleClass { var1 = 20, var2 = 200, var3 = "b"});

var dict = myList.ToDictionary(k => k.var1);
exampleClass item1 = dict[20];
类示例类
{
int-var1;
int-var2;
字符串变量3
}
列表myList=新列表()
Add(新示例类{var1=10,var2=100,var3=“a”});
Add(新示例类{var1=20,var2=200,var3=“b”});
var dict=myList.ToDictionary(k=>k.var1);
exampleClass item1=dict[20];
您可以使用该方法检索对象(或检索所有对象):

或在
列表中检索其索引:

myList.FindIndex(x => x.var1 == 20);
您可以使用该方法检索对象(或检索所有对象):

或在
列表中检索其索引:

myList.FindIndex(x => x.var1 == 20);

“假设var1的每个值都是唯一的,如果我使用一个以var1为键的字典,它会更快吗?”我不确定是否更快,但肯定会更简单。你为什么要寻找索引?你是想对它进行排序还是仅仅检索它?检索它。我可以用Dictionary和var1作为键来做同样的事情,但是我想知道我是否可以避免为我拥有的每个列表构建一个字典。“假设var1的每个值都是唯一的,那么如果我用一个Dictionary和var1作为键来代替它会更快吗”我不确定更快,但这肯定要简单得多。你为什么要找索引?你是想对它进行排序还是仅仅检索它?检索它。我可以用Dictionary和var1作为键来做同样的事情,但我想知道是否可以避免为我拥有的每个列表构建字典。
Find()
返回一个实际对象,他想要索引。@Zero21xxx:Right,
FindIndex
然后。
Find()
返回一个实际对象,他想要索引。@Zero21xxx:Right,
FindIndex
then.btw,
FindIndex
不在LINQ中,只在
System.Collections.Generic
中。顺便说一句,
FindIndex
不在LINQ中,只在
System.Collections.Generic
中。哇,谢谢链接。其中的一些内容超出了我基本的C#理解,但它为我提供了进一步研究的指导。哇,谢谢你的链接。其中的一些内容超出了我对C#的初步理解,但它给了我进一步研究的方向。
var index = myList.FindIndex(val => val.Var1 == 20);