简体   繁体   English

如何使用 linq.js 编写可枚举的“喜欢”查询

[英]How to write Enumerable 'like' query using linq.js

I have JSON array like below.我有如下 JSON 数组。


[
  {
    "_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.使用这个 JSON 数组,我需要使用 like 运算符过滤员工。 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();

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>

I also found method too..我也找到了方法..

var qryResult = Enumerable.From(_gramrTree)
    .Where("!!$._Text.match(/^"Emp"/i)")
    .OrderBy("$._Text")
    .Select("$._Text")
    .ToArray()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM