Open
Description
Enumerable.prototype.find = function (compareSelector) {
compareSelector = Utils.createLambda(compareSelector);
var result = null;
this.forEach(function(item) {
if (compareSelector(item)) {
result = item;
return;
}
});
return result;
};
Usage:
var result = Enumerable.from(list).find("x => x.Id == " + somevariable.Id);
It works fine and I hope you like it. It could use some optimization of code quality though.
I don't like the "x => x.Id == " + somevariable.Id part, because the somevariable.Id is outside of the quotation marks. Maybe you got a better solution for this problem.
C# Equivalent would be:
public static T Find<T>(this IList<T> source, Func<T, bool> condition)
{
foreach (var item in source)
{
if (condition(item))
{
return item;
}
}
return default(T);
}
Usage:
var result = list.Find(x => x.Id == somevariable.Id);
Metadata
Metadata
Assignees
Labels
No labels