javascriptlinq.js

linqjs intersect comparer issue


I am using linqjs and I have one array full of ids to include in a list, and an array full of complex objects which have a property userId.

Problem is when I do an intersection it never seems to return anything, however there is very little information around the compareSelector.

So here is an example of what I am doing:

enumerableOfUsers.intersect(listOfIdsToInclude, "$.userId");

So in the above example enumerableOfUsers would be an existing enumerable created from an array of users (which contain the userId field), the listOfIdsToInclude is an array of id values, like ["12345", "213213", "2124"] etc.

The intersect seems to work but never returns anything and I know the userIds match so am I doing anything wrong here?


Solution

  • The thing is that the compare selector is applied to items of both the first and second sets. The second set is a list of ids already so the compare selector doesn't apply. The projection yields undefined values which will always result in no results found.

    You need to apply the selector only to the first set of values. Try this instead:

    // using linqjs 2.x syntax
    var query = enumerableOfUsers.Select("$.userId").Intersect(listOfIdsToInclude);