[英]How to apply complex odata filters programatically with kendo data sources?
[英]How to apply complex data filters like SQL where clause on JSON
我正在使用Json在客戶端(javascript)表示數據,我將.net datatable對象轉換為json,現在需要對Json數據應用過濾器。 我使用_.where來應用基本數據過濾器,但需要像這樣的復雜過濾:
where = (stundentID = 55 and school='abc') AND (City='xyz' or City ='abc')
它是我們在Oracle或sql服務器查詢中使用的基本過濾類型。 現在,我如何使用underscore.js或Json或javascript結構上的某些東西應用這種機制。 我可能還需要使用JavaScript的聚合[Sum(sales)== 500]進行查詢以獲取前N個記錄。 等待您的寶貴建議。
謝謝,
假設您將此JSON表示為常規JavaScript對象-畢竟沒有JSON對象之類的東西-最好是使用linq.js
您將可以執行以下操作:
var studentFilteredArray =
Enumerable.From(students)
.Where(function(s) { return s.studenId == 55; })
.OrderBy(function(s) { return s.LastName; })
.ToArray();
或從上面的示例中:
.Where(function(s) {
return s.studentID === 55 && s.school === 'abc' && (s.City === 'xyz' || s.City === 'abc')
});
您也可以嘗試使用alasql.js ,在其中可以使用標准的SQL SELECT WHERE語句。 Alasql專門設計用於處理JSON數組,例如數據庫中的表。
// Create database and describe the table
var db = new alasql.Database();
db.exec('CREATE TABLE students ([studentID] INT, school STRING, [City] STRING)');
// Assign you array here
db.tables.students.data = [
{studentID: 55, school: 'abc', City:'abc'},
{studentID: 56, school: 'klm', City:'xyz'},
{studentID: 57, school: 'nyz', City:'xyz'}
];
// Use regular SQL
console.log(db.exec("SELECT * FROM students "
+" WHERE ([studentID] = 55 AND school='abc') AND ([City]='xyz' OR [City] ='abc')"));
在jsFiddle中嘗試此示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.