It's the first time im working with protobuf, and I wonder if there is any way to get access to a certain item inside a Repeated Field.
I created a method that will iterate through all the items, check an item field, and return it (i cannot return the pointer to it :( ).
public Appearance findItem(int itemID) {
foreach (Appearance item in appearances.Object) {
if (item.Id == itemID) {
return item;
}
}
return null;
}
It seems there is no find method to use some lambda expressions.
Is there any other way to achieve this? It would be perfect if i could hav pointer to the item, not a copy of it, so if i change it, i can write the complete repeated field directly.
It would be perfect if i could hav pointer to the item, not a copy of it, so if i change it, i can write the complete repeated field directly.
You can't do that, but you could return the index of the element instead. Given that repeated fields implement IEnumerable<T>
, you should be able to use LINQ easily enough. For example:
// index is an "int?" with a value of null if no items matched the query
var index = repeatedField
// You could use tuples here if you're using C# 7. That would be more efficient.
.Select((value, index) => new { value, (int?) index })
// This is whatever condition you want
.Where(pair => pair.value.SomeField == "foo")
.Select(pair => pair.index)
.FirstOrDefault();