搜索字符串数组的列表以在匹配元素中查找值并返回同一数组中的另一个元素

时间:2021-10-25 21:23:31

So I have

所以我有

    List<string[]> listy = new List<string[]>();

    listy.add('a','1','blue');
    listy.add('b','2','yellow');

And i want to search through all of the list ti find the index where the array containing 'yellow' is, and return the first element value, in this case 'b'.

我想搜索所有列表,找到包含'yellow'的数组所在的索引,并返回第一个元素值,在本例中为'b'。

Is there a way to do this with built in functions or am i going to need to write my own search here?

是否有办法使用内置函数执行此操作,或者我需要在此处编写自己的搜索?

Relatively new to c# and not aware of good practice or all the built in functions. Lists and arrays im ok with but lists of arrays baffles me somewhat.

相对较新的c#并且不了解良好实践或所有内置函数。列表和数组都没问题,但数组列表让我感到困惑。

Thanks in advance.

提前致谢。

4 个解决方案

#1


2  

As others have already suggested, the easiest way to do this involves a very powerful C# feature called LINQ ("Language INtegrated Queries). It gives you a SQL-like syntax for querying collections of objects (or databases, or XML documents, or JSON documents).

正如其他人已经建议的那样,最简单的方法是使用一个非常强大的C#功能,称为LINQ(“语言集成查询”)。它为您提供了一种类似SQL的语法,用于查询对象(或数据库,XML文档或JSON)的集合。文档)。

To make LINQ work, you will need to add this at the top of your source code file:

要使LINQ正常工作,您需要在源代码文件的顶部添加它:

using System.Linq;

使用System.Linq;

Then you can write:

然后你可以写:

IEnumerable<string> yellowThings = 
    from stringArray in listy
    where stringArray.Contains("yellow")
    select stringArray[0];

Or equivalently:

或者等效地:

IEnumerable<string> yellowThings =
    listy.Where(strings => strings.Contains("yellow"))
         .Select(strings => strings[0]);

At this point, yellowThings is an object containing a description of the query that you want to run. You can write other LINQ queries on top of it if you want, and it won't actually perform the search until you ask to see the results.

此时,yellowThings是一个对象,包含您要运行的查询的描述。如果需要,您可以在其上编写其他LINQ查询,并且在您要求查看结果之前,它实际上不会执行搜索。

You now have several options...

你现在有几种选择......

Loop over the yellow things:

绕过黄色的东西:

foreach(string thing in yellowThings)
{
    // do something with thing...
}

(Don't do this more than once, otherwise the query will be evaluated repeatedly.)

(不要多次执行此操作,否则将重复评估查询。)

Get a list or array :

获取列表或数组:

List<string> listOfYellowThings = yellowThings.ToList();
string[] arrayOfYellowThings = yellowThings.ToArray();

If you expect to have exactly one yellow thing:

如果你希望只有一个黄色的东西:

string result = yellowThings.Single();
// Will throw an exception if the number of matches is zero or greater than 1

If you expect to have either zero or one yellow things:

如果你希望有零或一个黄色的东西:

string result = yellowThings.SingleOrDefault();
// result will be null if there are no matches.
// An exception will be thrown if there is more than one match.

If you expect to have one or more yellow things, but only want the first one:

如果你希望有一个或多个黄色的东西,但只想要第一个:

string result = yellowThings.First();
// Will throw an exception if there are no yellow things

If you expect to have zero or more yellow things, but only want the first one if it exists:

如果你希望有零个或多个黄色的东西,但只想要第一个如果它存在:

string result = yellowThings.FirstOrDefault();
// result will be null if there are no yellow things.

#2


3  

Based on the problem explanation provided by you following is the solution I can suggest.

基于您提供的问题解释,我可以提出解决方案。

List<string[]> listy = new List<string[]>();

listy.Add(new string[] { "a", "1", "blue"});
listy.Add(new string[] { "b", "2", "yellow"});

var target = listy.FirstOrDefault(item => item.Contains("yellow"));

if (target != null)
{
    Console.WriteLine(target[0]);
}

This should solve your issue. Let me know if I am missing any use case here.

这应该可以解决您的问题。如果我在这里遗漏任何用例,请告诉我。

#3


2  

You might consider changing the data structure,

您可以考虑更改数据结构,

Have a class for your data as follows,

为您的数据提供如下课程,

public class Myclas
        {
            public string name { get; set; }
            public int id { get; set; }
            public string color { get; set; }
        }

And then,

接着,

static void Main(string[] args)
{
   List<Myclas> listy = new List<Myclas>();
   listy.Add(new Myclas { name = "a", id = 1, color = "blue" });
   listy.Add(new Myclas { name = "b", id = 1, color = "yellow" });
   var result = listy.FirstOrDefault(t => t.color == "yellow");
}

#4


1  

Your current situation is

你现在的情况是

List<string[]> listy = new List<string[]>();

listy.Add(new string[]{"a","1","blue"});
listy.Add(new string[]{"b","2","yellow"});

Now there are Linq methods, so this is what you're trying to do

现在有Linq方法,所以这就是你要做的

var result = listy.FirstOrDefault(x => x.Contains("yellow"))?[0];

#1


2  

As others have already suggested, the easiest way to do this involves a very powerful C# feature called LINQ ("Language INtegrated Queries). It gives you a SQL-like syntax for querying collections of objects (or databases, or XML documents, or JSON documents).

正如其他人已经建议的那样,最简单的方法是使用一个非常强大的C#功能,称为LINQ(“语言集成查询”)。它为您提供了一种类似SQL的语法,用于查询对象(或数据库,XML文档或JSON)的集合。文档)。

To make LINQ work, you will need to add this at the top of your source code file:

要使LINQ正常工作,您需要在源代码文件的顶部添加它:

using System.Linq;

使用System.Linq;

Then you can write:

然后你可以写:

IEnumerable<string> yellowThings = 
    from stringArray in listy
    where stringArray.Contains("yellow")
    select stringArray[0];

Or equivalently:

或者等效地:

IEnumerable<string> yellowThings =
    listy.Where(strings => strings.Contains("yellow"))
         .Select(strings => strings[0]);

At this point, yellowThings is an object containing a description of the query that you want to run. You can write other LINQ queries on top of it if you want, and it won't actually perform the search until you ask to see the results.

此时,yellowThings是一个对象,包含您要运行的查询的描述。如果需要,您可以在其上编写其他LINQ查询,并且在您要求查看结果之前,它实际上不会执行搜索。

You now have several options...

你现在有几种选择......

Loop over the yellow things:

绕过黄色的东西:

foreach(string thing in yellowThings)
{
    // do something with thing...
}

(Don't do this more than once, otherwise the query will be evaluated repeatedly.)

(不要多次执行此操作,否则将重复评估查询。)

Get a list or array :

获取列表或数组:

List<string> listOfYellowThings = yellowThings.ToList();
string[] arrayOfYellowThings = yellowThings.ToArray();

If you expect to have exactly one yellow thing:

如果你希望只有一个黄色的东西:

string result = yellowThings.Single();
// Will throw an exception if the number of matches is zero or greater than 1

If you expect to have either zero or one yellow things:

如果你希望有零或一个黄色的东西:

string result = yellowThings.SingleOrDefault();
// result will be null if there are no matches.
// An exception will be thrown if there is more than one match.

If you expect to have one or more yellow things, but only want the first one:

如果你希望有一个或多个黄色的东西,但只想要第一个:

string result = yellowThings.First();
// Will throw an exception if there are no yellow things

If you expect to have zero or more yellow things, but only want the first one if it exists:

如果你希望有零个或多个黄色的东西,但只想要第一个如果它存在:

string result = yellowThings.FirstOrDefault();
// result will be null if there are no yellow things.

#2


3  

Based on the problem explanation provided by you following is the solution I can suggest.

基于您提供的问题解释,我可以提出解决方案。

List<string[]> listy = new List<string[]>();

listy.Add(new string[] { "a", "1", "blue"});
listy.Add(new string[] { "b", "2", "yellow"});

var target = listy.FirstOrDefault(item => item.Contains("yellow"));

if (target != null)
{
    Console.WriteLine(target[0]);
}

This should solve your issue. Let me know if I am missing any use case here.

这应该可以解决您的问题。如果我在这里遗漏任何用例,请告诉我。

#3


2  

You might consider changing the data structure,

您可以考虑更改数据结构,

Have a class for your data as follows,

为您的数据提供如下课程,

public class Myclas
        {
            public string name { get; set; }
            public int id { get; set; }
            public string color { get; set; }
        }

And then,

接着,

static void Main(string[] args)
{
   List<Myclas> listy = new List<Myclas>();
   listy.Add(new Myclas { name = "a", id = 1, color = "blue" });
   listy.Add(new Myclas { name = "b", id = 1, color = "yellow" });
   var result = listy.FirstOrDefault(t => t.color == "yellow");
}

#4


1  

Your current situation is

你现在的情况是

List<string[]> listy = new List<string[]>();

listy.Add(new string[]{"a","1","blue"});
listy.Add(new string[]{"b","2","yellow"});

Now there are Linq methods, so this is what you're trying to do

现在有Linq方法,所以这就是你要做的

var result = listy.FirstOrDefault(x => x.Contains("yellow"))?[0];