[英]Find string value in comma-separated list
I have a list(string) with members in the form of 'label,location'; 我有一个列表(字符串),其成员的形式为'label,location'; labels are distinct.
标签是不同的。 I need a method which accepts a label parameter and returns a location.
我需要一个接受label参数并返回位置的方法。
I can iterate through using foreach to find the correct label, then manipulate the list member using Split to return the location. 我可以使用foreach迭代查找正确的标签,然后使用Split操作列表成员以返回位置。 However I'm sure there's a better way, presumably using LINQ, along the lines of
但是我确信有更好的方法,大概是使用LINQ,沿着这个方向
return theList.Single(x => x == theLabel);
This doesn't work however since the list values == label,location. 但这不起作用,因为列表值== label,location。
See the code below: 请参阅以下代码:
string get_location(List<string> list, label)
{
return list.Select(s => s.Split(',')).ToDictionary(s => s[0], s => s[1])[label];
}
If there are multiple requests on the same list, then it is better to save that dictionary and then reuse for all labels queried: 如果同一列表中有多个请求,则最好保存该字典,然后重新使用查询的所有标签:
var map = list.Select(s => s.Split(',')).ToDictionary(s => s[0], s => s[1]);
Alternatively: 或者:
var map = new Dictionary<string, string>();
list.ForEach(s => { var split = s.Split(','); map.Add(split[0], split[1]); });
Since the labels are unique, you might consider converting your data to a dictionary<string,string>
. 由于标签是唯一的,您可以考虑将数据转换为
dictionary<string,string>
。 You can keep the label as the key and location as the value . 您可以将标签作为键和位置作为值 。
var lableLocatonDict = theList.Select(item => item.Split(','))
.ToDictionary(arr => arr[0], arr => arr[1]);
Now to access a location(value) for a specific label(key) you can simply do this 现在,要访问特定标签(键)的位置(值),您只需执行此操作即可
var location = lableLocatonDict["LabelToCheck"];
You can use the ContainsKey method if you want to check whether an item exists in the dictionary before accessing it. 如果要在访问之前检查字典中是否存在项,则可以使用ContainsKey方法。
if(lableLocatonDict.ContainsKey("LabelToCheck"))
{
var location = lableLocatonDict["LabelToCheck"];
}
Or the TryGetValue
或者
TryGetValue
var location = string.Empty;
if(lableLocatonDict.TryGetValue("LabelToCheck",out location))
{
// location will have the value here
}
As I and the other 2 answers recommend, Dictionary was designed for exactly this purpose. 正如我和其他2个答案所推荐的那样,Dictionary就是为了这个目的而设计的。 You expressed concern with iterating a dict as opposed to a list thinking it could be more difficult, but in fact it is easier since no splitting would be needed (and faster).
你表示担心迭代dict而不是列表认为它可能更难,但事实上它更容易,因为不需要分裂(并且更快)。
Dictionary<String,String> locations = new Dictionary<String,String>();
//How to add locations
locations.Add("Sample Label","Sample Location");
//How to modify a location
locations["Sample Label"] = "Edited Sample Locations";
//Iterate locations
foreach (var location in locations)
{
Console.WriteLine(location.key);
Console.WriteLine(location.value);
}
I would even go a bit further and say to future proof your application and add the ability for more information to be stored in each location, you should really be using ObservableCollection<T>
where T
is a custom class object: 我甚至会进一步说明你的应用程序的未来证明并添加能够存储在每个位置的更多信息,你应该真正使用
ObservableCollection<T>
其中T
是一个自定义类对象:
public class LocationInfo
{
String Label {get;set;}
String Location {get;set;}
String Description {get;set;}
}
ObservableCollection<LocationInfo> Locations = new ObservableCollection<LocationInfo>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.