Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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# 使用linq在数组列表中查找项_C#_Arrays_Linq - Fatal编程技术网

C# 使用linq在数组列表中查找项

C# 使用linq在数组列表中查找项,c#,arrays,linq,C#,Arrays,Linq,我有一个数组列表,每个数组中有2个字符串。我需要检查每个数组中的第一个字符串是否与给定的数字匹配。我认为linq是实现这一点的最佳方式。我在这里找到了一个有用的答案: 它表示要在列表中查找项目,请执行以下操作: string result = myList.Single(s => s == search); 从评论中,我想我想使用SingleOrDefault。但是如何让它搜索列表中每个数组的第一项呢? 以下是我的阵列列表: List<string[]> shipsLater

我有一个数组列表,每个数组中有2个字符串。我需要检查每个数组中的第一个字符串是否与给定的数字匹配。我认为linq是实现这一点的最佳方式。我在这里找到了一个有用的答案: 它表示要在列表中查找项目,请执行以下操作:

string result = myList.Single(s => s == search);
从评论中,我想我想使用SingleOrDefault。但是如何让它搜索列表中每个数组的第一项呢? 以下是我的阵列列表:

List<string[]> shipsLater = new List<string[]> {};    
string[] itemArr = { item.pid, future };
shipsLater.Add(itemArr);
List shipsLater=新列表{};
字符串[]itemArr={item.pid,future};
发货人。添加(itemArr);

因此您有一个数组列表,如:

List<string[]> list = new List<string[]>();
这将为您提供列表中与您的条件匹配的所有元素

从您的:

基本上是真是假,我找到了没有

如果您只想返回一个布尔结果,指示该条件是否满足,请使用
可枚举。任何
类似:

bool result = list.Any(arr=> arr.First() == search);
bool result = list.Any(arr=> arr.First() == search.ToString());
如果参数的类型为
int
,则调用
ToString
如下:

bool result = list.Any(arr=> arr.First() == search);
bool result = list.Any(arr=> arr.First() == search.ToString());

因此,您有一个数组列表,如:

List<string[]> list = new List<string[]>();
这将为您提供列表中与您的条件匹配的所有元素

从您的:

基本上是真是假,我找到了没有

如果您只想返回一个布尔结果,指示该条件是否满足,请使用
可枚举。任何
类似:

bool result = list.Any(arr=> arr.First() == search);
bool result = list.Any(arr=> arr.First() == search.ToString());
如果参数的类型为
int
,则调用
ToString
如下:

bool result = list.Any(arr=> arr.First() == search);
bool result = list.Any(arr=> arr.First() == search.ToString());

您可以使用
Dictionary()
获得最佳性能结果;如果要使用
string[]
请使用以下命令:

string result = myList.Single(s => s[0] == search);

您可以使用
Dictionary()
获得最佳性能结果;如果要使用
string[]
请使用以下命令:

string result = myList.Single(s => s[0] == search);

您的初始数组结构有问题。仔细地转换到字典突然使实现变得简单

List<string[]> shipsLater = new List<string[]>
{
  new []{ "pid1", "abc" },
  new []{ "pid1", "xyz" },
  new []{ "pid2", "123" }
};

Dictionary<string, IEnumerable<string>> lookupByPid = shipsLater
  .GroupBy(g => g[0])
  .ToDictionary(k => k.Key, v => v.Select(i => i[1]));

// now your lookups are as simple as...
IEnumerable<string> matches = lookupByPid["pid1"];
List shipsLater=新列表
{
新[]{“pid1”,“abc”},
新[]{“pid1”,“xyz”},
新[]{“pid2”,“123”}
};
字典lookupypid=shipsLater
.GroupBy(g=>g[0])
.ToDictionary(k=>k.Key,v=>v.Select(i=>i[1]);
//现在,您的查找非常简单。。。
IEnumerable matches=lookupypid[“pid1”];

您的初始数组结构有问题。仔细地转换到字典突然使实现变得简单

List<string[]> shipsLater = new List<string[]>
{
  new []{ "pid1", "abc" },
  new []{ "pid1", "xyz" },
  new []{ "pid2", "123" }
};

Dictionary<string, IEnumerable<string>> lookupByPid = shipsLater
  .GroupBy(g => g[0])
  .ToDictionary(k => k.Key, v => v.Select(i => i[1]));

// now your lookups are as simple as...
IEnumerable<string> matches = lookupByPid["pid1"];
List shipsLater=新列表
{
新[]{“pid1”,“abc”},
新[]{“pid1”,“xyz”},
新[]{“pid2”,“123”}
};
字典lookupypid=shipsLater
.GroupBy(g=>g[0])
.ToDictionary(k=>k.Key,v=>v.Select(i=>i[1]);
//现在,您的查找非常简单。。。
IEnumerable matches=lookupypid[“pid1”];

您希望得到什么结果?我希望在item.pid中搜索给定字符串,例如“9022”。最简单的是
shipsLater[0][0]==shipsLater[1][0]
;)你还没有回答我的问题,结果会怎样?数组列表?单个阵列?一个字符串?哦,基本上是真还是假,我找到了还是没找到?结果是什么?我想搜索所有item.pid中给定的字符串,例如“9022”。最简单的是
shipsLater[0][0]==shipsLater[1][0]
;)你还没有回答我的问题,结果会怎样?数组列表?单个阵列?一个字符串?哦,基本上是真的还是假的,我找到了没有?只能有一个或没有。我可以使用SingleOrDefault而不是Where吗?@dmikester1,我想澄清一下,您的意思是列表中有一项符合条件?如果是这种情况,请务必使用
SingleOrDefault
,但它将返回一个
字符串[]
,其中第一项将与您的搜索条件匹配。只能有一个或没有。我可以使用SingleOrDefault而不是Where吗?@dmikester1,我想澄清一下,您的意思是列表中有一项符合条件?如果是这种情况,请务必使用
SingleOrDefault
,但它将返回一个
字符串[]
,其中第一项将与您的搜索条件匹配。