Is there a way to find the index from list of partial prefixes with Linq, something like:
List<string> PartialValues = getContentsOfPartialList();
string wholeValue = "-moz-linear-gradient(top, #1e5799 0%, #7db9e8 100%)";
int indexOfPartial = PartialValues
.IndexOf(partialPrefix=>wholeValue.StartsWith(partialPrefix));
Unfortunately, IndexOf()
doesn't accept lambda expression. Is there a similar Linq method for this?
You don't need LINQ at all, List<T>
has a method FindIndex
.
int indexOfPartial = PartialValues
.FindIndex(partialPrefix => wholeValue.StartsWith(partialPrefix));
For the sake of completeness, you can use LINQ, but it's not necessary:
int indexOfPartial = PartialValues
.Select((partialPrefix , index) => (partialPrefix , index))
.Where(x => wholeValue.StartsWith(x.partialPrefix))
.Select(x => x.index)
.FirstOrDefault(-1);