|
08.03.2009, 08:23 | #1 |
Участник
|
AX Developer tips: LINQ in X++
Источник: http://axdevposts.blogspot.com/2009/03/linq-in-x.html
============== LINQ (Language INtegrated Query) was introduced in C# 3.0 and was created for unified SQL-like inquiry interface to any data source: database, XML files, or object collections. In C# it can be performed in SQL-like format or through extention methods: collection.Where(....), collection.Select(....) In X++ SQL for DB access is already implemented, but sometimes developers need to fetch objects from collections (Lists, Maps, Sets...) by some specific criteria. This is usualy performed iterating objects and comparing their properties to specific value. To simplify this approach I wrote small framework which allows to select objects from collections (List in my case) using predecative conditions. Lets imageine we have simple class QueryListTEST: class QueryListTEST { boolean flag; int value; str nam; } public Boolean parmFlag(Boolean _flag = flag) { ; flag = _flag; return flag; } public Str parmName(Str _name = nam) { ; if (!prmISDefault(_name)) { nam = _name; } return nam; } public Int parmValue(Int _value = value) { ; if (!prmISDefault(_value)) { value = _value; } return value; } public static QueryListTEST construct(str _name, boolean _flag, int _value) { QueryListTEST test = new QueryListTEST(); ; test.parmName(_name); test.parmFlag(_flag); test.parmValue(_value); return test; } It contains 3 properties (parmMethods with 1 parameter with default value). Lets initialize list of objects of type QueryListTEST: QueryList list = new QueryList(); ; list.addEnd(QueryListTEST::construct("", true, 0)); list.addEnd(QueryListTEST::construct("2", false, -1)); list.addEnd(QueryListTEST::construct("3", true, 2)); QueryList class is basically what I'm going to implement in this example. It extends List but has one extra method select found = list.select(new QueryListCondition_LessOrEq('parmFlag', false).and(new QueryListCondition_More('parmName', "2")); found is also list of objects of the same type. As you can see, list calls select method, passing condition to it. Condition is represented by instance of class QueryListCondition. It's an abstract class, which compares value of class instance property (first parameter in constructor) to some value (second parameter). Its child classes implement all possible comparison boolean operators: ==, !=, >, >=, _val2Compare; } return ret; }</span> protected boolean compareNumVal(real _numVal) { return _numVal > 0; } QueryListCondition_MoreOrEq public class QueryListCondition_MoreOrEq extends QueryListCondition { } protected boolean thisResult(anytype _val, anytype _val2Compare) { boolean ret; ; if (_val && !_val2Compare) { ret = this.numericProperty() ? this.compareNumVal(_val) : true; } else if (!_val && _val2Compare) { ret = this.numericProperty() ? !this.compareNumVal(_val2Compare) : false; } else if (!_val && !_val2Compare) { ret = true; } else { ret = _val >= _val2Compare; } return ret; } protected boolean compareNumVal(real _numVal) { return _numVal >= 0; } QueryListCondition_Less public class QueryListCondition_Less extends QueryListCondition { } <span style="font-size:85%;color:#009900;">protected boolean thisResult(anytype _val, anytype _val2Compare) { boolean ret; ; if (_val && !_val2Compare) { ret = this.numericProperty() ? this.compareNumVal(_val) : false; } else if (!_val && _val2Compare) { ret = this.numericProperty() ? !this.compareNumVal(_val2Compare) : true; } else if (!_val && !_val2Compare) { ret = false; } else { ret = _val <span style="font-size:85%;color:#009900;">protected boolean compareNumVal(real _numVal) { return _numVal QueryListCondition_LessOrEq public class QueryListCondition_LessOrEq extends QueryListCondition { } protected boolean thisResult(anytype _val, anytype _val2Compare) { boolean ret; ; if (_val && !_val2Compare) { ret = this.numericProperty() ? this.compareNumVal(_val) : false; } else if (!_val && _val2Compare) { ret = this.numericProperty() ? !this.compareNumVal(_val2Compare) : true; } else if (!_val && !_val2Compare) { ret = true; } else { ret = _val > _val2Compare; } return ret; } <span style="font-size:85%;color:#009900;">protected boolean compareNumVal(real _numVal) { return _numVal
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору. |
|
Теги |
linq |
|
|