2016年2月19日 星期五

linq.js - LINQ for JavaScript 使用範例

走訪物件
====
Enumerable.From(arr).Where(a=>a.voucherTime==null).ForEach(function (object) {
                    object.check = newValue;
                });

找出物件陣列中的某個物件
====
Enumerable.From(arr).Where(a=>a.id===id).FirstOrDefault() 

判斷字串陣列中是否包含某個字串
====
Enumerable.From(arr).Contains("ag-cell")

依據 key 替換物件
====
dic=Enumerable.From(arr).ToDictionary("$.id", "$"); // $ : 物件, $.id : 物件的key屬性
dic.Set(data.id,?); // 替換
arr=dic.ToEnumerable().Select("$.Value").ToArray(); // Dictionary 轉為 Enumerable 會是新物件集合,內含兩個屬性 key、value,$.Value 就是原本的物件

刪除某個物件
====
$scope.objects=Enumerable.From($scope.objects).Except([obj]).ToArray();

用函數作為條件
 .Where(function (x) {return new Date(x.expireDate)>=$scope.fromDate || $scope.fromDate==null})

官方文件 (載點已掛)


載入方式:
import Enumerable from '../../../scripts/linq1.min.js';

使用範例:
Enumerable.from(xxx)
.where(a => a.xxx==ooo)
.groupBy(a => a.xxx)
.orderBy(a => a.key())
.select(a => ({ name: a.first().xxx, cnt: a.sum(b => b.xxx)})).select(a => `${a.name}: ${a.cnt}`)
.toJoinedString(', ')

Entity Framework 建立新物件並儲存後馬上取得關聯資料

使用 CreateProxy 建立物件,不要直接 new var newmodel = _contextXXX.CreateProxy<yyy>(); ... _contextXXX.yyy.Add(newmodel); await _contextXXX.SaveC...