AXForum  
Вернуться   AXForum > Microsoft Dynamics AX > DAX Blogs
DAX
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 08.03.2009, 08:23   #1  
Blog bot is offline
Blog bot
Участник
 
25,559 / 848 (80) +++++++
Регистрация: 28.10.2006
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

 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
axStart: Microsoft Dynamics AX 2009 Hot Topics Web Seminar Series Blog bot DAX Blogs 0 06.08.2008 12:05
AX Developer tips: Dynamics AX Team Server Blog bot DAX Blogs 0 22.07.2008 21:05
AX Developer tips: Dynamics AX Performance issue. Cross object references drawbacks. Blog bot DAX Blogs 0 14.07.2008 00:11
AX Developer tips: Updating records without FORUDATE statement Blog bot DAX Blogs 0 03.07.2008 14:05
AX Developer tips: .NET enumerations in Dynamics AX Blog bot DAX Blogs 0 03.07.2008 14:05

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 13:29.
Powered by vBulletin® v3.8.5. Перевод: zCarot
Контактная информация, Реклама.