从结构数组中获取字符串数组C# [英] Getting an array of strings from an array of a structures C#
问题描述
我有一系列结构可以说
I have an array of structures lets say
struct info
{
string a{get;set;}
string b{get;set;}
int c{get;set;}
}
想象一下
And imagine that
info[] information;
是我拥有的信息数组。
但我想得到一个字符串'b'的数组。执行此操作的最佳方法是什么?在c#中有一种默认方式来处理此问题。我需要它,因为我编写了一个有用的函数来编译使用字符串的列表。
我知道我可以使用foreach编译一个列表但这是很多代码每次我想要结构数组中的项目列表时使用。
is the array of information i have.
but i want to get just an array of the strings 'b'. What is the best way to do this/ is there a default way to handle this in c#. I need it as i wrote a useful function for compiling lists that works with strings.
I understand i could compile a list using foreach but this is a lot of code to use each time i want a list of items in the structure array.
推荐答案
string[] sa = information.Select(I => I.b).ToArray();
我讨厌在平板电脑上输入代码......它自动修正sa到as[/ edit]
[edit]I hate typing code on a tablet... It autocorrected "sa" to "as"[/edit]
这篇关于从结构数组中获取字符串数组C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!