Search code examples
c#linqlinq.js

Linq to linq.js translation with value assignation


I am trying to assign a value with a linq.js expresion. The linq lamba expresion would be like this:

List<SearchSpaceRoom> roomsAvailable = Spaces.Select(s => 
   { 
       s.Available = s.Rooms.Where(r => r.RoomTypeRanges.Any(t => t.Type.ToString() == "Theatre" && t.Min <= 101 && t.Max >= 200)).Count(); 
       return s; 
   })
   .ToList();

But I cannot find the way to do this to work with linq.js. The idea is to have something like this:

var data = eval($('#SpaceJson').val());
var results = $.Enumerable.From(data).ToArray();
var layout = 'Theatre';
var minDelegates = '101';
var maxDelegates = '200';

results = $.Enumerable.From(results)
   .Select('{$.Available = $.Rooms.Where($.RoomTypeRanges.Any($.Type == \'' + layout + '\' && $.Min <= \'' + parseInt(minDelegates) + '\' && t.Max >= \'' + parseInt(maxDelegates) + '\')).Count(); return $; }').ToArray();

Is this possible, can anyone suggest me the best approach to do this?

If you need more information about the expression: it is to assign the amount of rooms that meets the requirements of Min and Max people for a certain layout (Classroom, Theatre) for each space. The basic structure is like:

  1. One space contains Rooms
  2. One Room contains the different layouts
  3. A layout contains the max an min people

I can provide all the data structure if you need but I believe that will confuse more than help.

Thanks!

EDIT: I add the classes here

public class SearchSpaceViewModel
{
    public List<SearchSpaceRoom> Spaces { get; set; }
    public string RoomsJson { get; set; }
}

public class SearchSpaceRoom
{
    public List<RoomItem> Rooms { get; set; }
    public int Available { get; set; }
    .
    .
    .
}

public class RoomItem
{
    public List<RoomTypeRange> RoomTypeRanges { get; set; }
    .
    .
    .

}

public class RoomTypeRange
{
    public string Type { get; set; }
    public int Max { get; set; }
    public int Min { get; set; }
}

Solution

  • Just to close this, I managed to do it using a mix of JQuery and linq.js:

     function ParseJson() {
            var data = eval($('#RoomsJson').val());
            var results = $.Enumerable.From(data).ToArray();
            var layout = 'Theatre';
            var intMin = 101;
            var intMax = 200;
    
            $.each(results, function (i, space) {
                var count = 0;
                $.each(space.Rooms, function (j, room) {
                    if ($.Enumerable.From(room.RoomTypeRanges).Any('$.Type == \'' + layout + '\' && $.Min <= ' + intMin + ' && $.Max >= ' + intMax)) {
                        count++;
                    }
                });
                space.Available = count;
            });
    
            return results;
        }
    

    I am sure that there are more elegant solutions but it seems to work fine.