Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ.JS, comparerSelector for Except

I'm trying to use LINQ.JS (not .Net LINQ!) and cannot get the comparerSelector concept.

I have two arrays of objects, every object contains Id,Name,Date,Passport etc.

I want to have a difference between them by the following 2 fields only: Id,Name. Difference in Date and Passport should be ignored. How to write the comparerSelector?

The following works for Id only:

Enumerable.From(p2)
    .Except(p1, "$.Id}")
    .ForEach(function (x) { alert('Id == ' + x.Id); });

This works too:

Enumerable.From(p2)
    .Except(p1, function(x) { return x.Id; })
    .ForEach(function (x) { alert('id == ' + x.Id); });

How to add the Name field to the comparer?

The following code doesn't work:

Enumerable.From(p2)
    .Except(p1, function(x) { return { Id : x.Id, Name : x.Name }; })
    .ForEach(function (x) { alert('Id == ' + x.Id); });

Regards,

like image 822
noober Avatar asked Aug 21 '13 14:08

noober


1 Answers

As with all comparators in LINQ.js, you should return a string or some other object that has a distinct string representation.

Personally, I would opt to return strings exclusively:

const query = Enumerable.From(p2)
    .Except(p1, "[$.Id, $.Name].join(':')")
    .ToArray();
like image 146
Jeff Mercado Avatar answered Sep 20 '22 22:09

Jeff Mercado