Using List and Linq withing javascript

Oct 21, 2014 at 6:03 PM
Edited Oct 21, 2014 at 6:53 PM

I am wondering if we are allowed to used linq functions like List.Exists within javascript.

Currently, I have this line
if (child.Name == "tr" && accountList.Exists(p => p == child.ChildNodes[1].InnerText)) 
{ }
But it's giving me a syntax error. accountList is passed in through
 engine.Script[key] = value; 
where key = "accountList" and the value is a List<string>

Is this something I can do, or do I have to do things the old fashioned way?
Oct 21, 2014 at 8:57 PM
Edited Oct 21, 2014 at 8:58 PM

The method you're trying to call from script code is declared like this:
public bool Exists(Predicate<T> match)
Because you're dealing with a string list, it requires an argument of type Predicate<string>, which is a delegate type.

The syntax p => p == child.ChildNodes[1].InnerText is C#-specific and not legal in JavaScript. Instead, you need to create the appropriate delegate around a script function.

To do that, first you need to expose the delegate type to the script engine:
engine.AddHostType("StrPred", typeof(Predicate<string>));
Now you can call the method from script code like this:
pred = function (p) { return p == child.ChildNodes[1].InnerText; };
exists = accountList.Exists(new StrPred(pred));
Good luck!