2

I have JSON array like below.


[
  {
    "_Id": "0001",
    "_PatentId": "0000",
    "_Text": "Employee",
    "_Value": "employee",
    "_PermissionLevel": 55      
  },
  {
    "_Id": "0002",
    "_PatentId": "0000",
    "_Text": "Employees",
    "_Value": "employees",
    "_PermissionLevel": 55
  },
 {
    "_Id": "0002",
    "_PatentId": "0001",
    "_Text": "Dept",
    "_Value": "Dept",
    "_PermissionLevel": 55
  }
]

Using this JSON array, I need to filter employees using like operator. I have used query below and it's working fine.

var qryResult = Enumerable.From(_gramrTree).Where("$._Text == 'Employee'").OrderBy("$._Id").Select("$._Id").ToArray();

But I need to do with like operator.. but it's not working..

Unsuccessful queries

var qryResult = Enumerable.From(_gramrTree).Where("$._Text like '%Emp%'").OrderBy("$._Id").Select("$._Id").ToArray();

var qryResult = Enumerable.From(_gramrTree).Where("$._Text % 'Emp'").OrderBy("$._Id").Select("$._Id").ToArray();
nubteens
  • 5,462
  • 4
  • 20
  • 31
isanka thalagala
  • 456
  • 2
  • 10
  • 22

2 Answers2

2

please try this workaround (it seems, that like is not included)

.Where("~($._Text).toUpperCase().indexOf('emp'.toUpperCase())")

Working example:

var _gramrTree = [{ "_Id": "0001", "_PatentId": "0000", "_Text": "Employee", "_Value": "employee", "_PermissionLevel": 55 }, { "_Id": "0002", "_PatentId": "0000", "_Text": "Employees", "_Value": "employees", "_PermissionLevel": 55 }, { "_Id": "0002", "_PatentId": "0001", "_Text": "Dept", "_Value": "Dept", "_PermissionLevel": 55 }],
    qryResult = Enumerable.From(_gramrTree).Where("~($._Text).toUpperCase().indexOf('emp'.toUpperCase())").ToArray();

document.write('<pre>' + JSON.stringify(qryResult, 0, 4) + '</pre>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>
TheBuzzSaw
  • 8,648
  • 5
  • 39
  • 58
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

I also found method too..

var qryResult = Enumerable.From(_gramrTree)
    .Where("!!$._Text.match(/^"Emp"/i)")
    .OrderBy("$._Text")
    .Select("$._Text")
    .ToArray()
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
isanka thalagala
  • 456
  • 2
  • 10
  • 22