how to get static range in your report - dynamics-ax-2009

In AX Report the User can select different Range. I seek to show that the criteria used to filter user in the report itself even
the expression "parameter!CustomerAccount.Value" don't work because this filter is not a static parameter.
the user can use any table from the query and any field from the table and any criteria.. I'm locking for trick to get for which table, which field and what criteria it uses.

this method work very well ^_^
(( I use it privet and not static ))
static void getQueryRanges2(Args _args)
{
Query query;
QueryRun queryRun;
QueryBuildDataSource qbd;
QueryBuildRange range;
QueryFilter filter;
int cnt, filtercnt, i,j, k;
DictTable dictTable;
DictField dictField;
str fieldLabel;
;
query = new query(queryStr(smmSalesCustItemStatistics));
queryRun = new QueryRun(query);
queryRun.prompt();
query = queryRun.query(); for(i = 1; i <= query.dataSourceCount(); i++)
{
cnt = query.dataSourceNo(i).rangeCount();
filtercnt = 0;
if(!query.dataSourceNo(i).embedded())
{
filtercnt = query.queryFilterCount(query.dataSourceNo(i));
}
dictTable = new DictTable(query.dataSourceNo(i).table());
for (k=1; k<= filtercnt; k++)
{
filter = query.queryFilter(k, query.dataSourceNo(i));
dictField = new DictField(query.dataSourceNo(i).table(), fieldname2id(query.dataSourceNo(i).table(), filter.field()));
info (strFmt("%1, %2. Range = %3", dictTable.label(), dictField.label(), filter.value()));
}
for (j=1; j<=cnt; j++)
{
range = queryRun.query().dataSourceNo(i).range(j);
dictField = new DictField(query.dataSourceNo(i).table(), fieldname2id( query.dataSourceNo(i).table(), range.AOTname()));
if(range.value())
{
info(strfmt("%1, %2. Range = %3",dictTable.label(), dictField.label(), range.value()));
}
}
}
}
enjoy :)

Related

Best and easiest way to export all the data from a datatable to an excel to get it downloaded using C#?

Using C#, which is the best and easiest way to export all the data from a datatable to an excel to get it downloaded?
I use Microsoft Interop.
Here is some sample code to get you started.
There are probably hundreds of tutorials which are better than the below. I have removed much of the code I use so I doubt the following will compile, but with careful reading, and some investigation into tutorials, you should be able to accomplish what you need.
public static void ExportToExcel(DataSet dsTodo, string strPathToSaveFile)
{
try
{
int numSteps = (dsTodo != null ? dsTodo.Tables.Count * 2 : 1);
Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
Workbook xlWorkbook = ExcelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
if (dsTodo.Tables.Count > 0)
{
for (int i = dsTodo.Tables.Count; i > 0; i--)
{
Sheets xlSheets = null;
Worksheet xlWorksheet = null;
//Create Excel Sheets
xlSheets = ExcelApp.Sheets;
xlWorksheet = (Worksheet)xlSheets.Add(xlSheets[1], Type.Missing, Type.Missing, Type.Missing);
System.Data.DataTable table = dsTodo.Tables[i - 1];
xlWorksheet.Name = table.TableName;
for (int j = 1; j < table.Columns.Count + 1; j++)
{
ExcelApp.Cells[1, j] = table.Columns[j - 1].ColumnName;
}
var excelData = new object[table.Rows.Count, table.Columns.Count];
for (int rowJ = 0; rowJ < table.Rows.Count; rowJ++)
{
for (int colI = 0; colI < table.Columns.Count; colI++)
{
excelData[rowJ, colI] = table.Rows[rowJ][colI];
}
}
int startCol = 1;
int endCol = startCol + table.Columns.Count - 1;
int startRow = 2;
int endRow = startRow + table.Rows.Count - 1;
string startLoc = GetCellFromCoords(startCol, startRow);
string endLoc = GetCellFromCoords(endCol, endRow);
//ExcelApp.get_Range(startLoc, endLoc).Value2 = excelData;
try
{
Range valRange = ExcelApp.get_Range(startLoc, endLoc);
valRange.Value2 = excelData;
}

Count number of emails in outlook folder

I want to count the number of mails i received which has word say 'xyz' in subject line.But I do not want it to count duplicates, I mean if I get a mail with subject 'xyz' and I reply to it including myself I want that to be counted as 1 not 2. Is it possible in outlook?
Yeah that's possible definetly, you did not specify the techno you would use but a sample code using interop could be:
var outlookApp = new Outlook.Application();
var mapi = outlookApp.GetNamespace("MAPI")
var root = mapi.GetDefaultFolder(OlDefaultfolders.olFolderInbox)
var folders = root.Folders;
int count = 0;
for(int i = folders.Count; i >= 1; i--)
{
var folder = folders[i];
count += GetCount(folder, 0);
Marshal.ReleaseComObject(folder);
}
Marshal.ReleaseComObject(folders);
//... function
private int GetCount(MAPIFolder folder, int count)
{
var folderItems = folder.Items;
if(folderItems != null && folderItems.Count >= 1)
{
for(int i = folderItems.Count; i >= 1; i--)
{
//Check item.Subject for string logic
count++;
//Add item in collection variable to check duplicates,
//generate an id with subject + creationtime for tracking?
}
}
if(folder.Folders != null && folder.Folders >= 1)
{
var subFolders = folder.Folders;
for(int i = subfolders.Count; i >= 1; i--)
{
var subFolder = subFolders[i]
count += GetCount(subFolder, count);
Marshal.ReleaseComObject(subfolder);
}
}
Marshal.ReleaseComObject(folderItems);
return count;
}
Hope it helps someone,

Nested for loop breaks to Linq

I'm pretty new to Linq and I am having troubles with creating the Linq equivalent of the below nested for loops:
for(int i = 0; i < toCompare1.Length; i++)
{
bool isUnique = true;
for(int j = 0; j < toCompare2.Length; j++)
{
if(toCompare1[i].Contains(toCompare2[j]))
{
isUnique = false;
break;
}
}
if(isUnique == true)
{
uniqueValues.Add(toCompare1[i]);
}
}
Currently, my draft code is something like this:
var unique =
from c1 in toCompare1
from c2 in toCompare2
where !c1.Contains(c2)
select c1;
But it duplicates the entries I want to have.
Can anyone help me with this?
Thanks!
In fluent syntax:
toCompare1.Where(item => !toCompare2.Any(item2 => item.Contains(item2)))
In query sytax:
from item1 in toCompare1
where !toCompare2.Any(item2 => item1.Contains(item2))
select item1;

Sorting a DataTable using LINQ

I have a DataTable which has the following structure.
StartDate (Type DateTime)
EndDate (Type DateTime)
Description (Type Text)
What I need to check is if the dates overlap or not, i.e., if the Start Date of 1 item is less than the EndDate of the previous item.
Here is the code I wrote.
NB: I already have checks to ensure that for each record, the Start Date has to be lesser than the End Date
dtDates.DefaultView.Sort = "StartDate ASC"; //Sort the DataTable based on the Start Dates
if(dtDates.Rows.Count > 1) //Check only if more than 1 Date is specified
{
for (int i = 1; i < dtDates.Rows.Count; i++)
{
if(TypeConvert.ToDate(dtDates.Rows[i]["StartDate"] < TypeConvert.ToDate(dtDates.Rows[i-1]["EndDate"])
{
retVal = true;
currentRow = i;
break;
}
}
}
The above code works fine.
But now, I want to implement it using LINQ
So, here is the code I am trying to attempt.
How can I do the whole comparison using LINQ?
public static bool CheckIfDatesOverlap(DataTable dataTable)
{
bool retVal = false;
int currentRow = 0;
List<DataRow> listDates = (from DataRow dgv in dataTable.Rows
select dgv).OrderBy(x => x[StartDate]).ToList();
if (listDates.Count > 1) //Perform Comparision only if more than 1 row is present
{
for (int i = 1; i < listDates.Count; i++)
{
if (TypeConvert.ToDateTime(listDates[i][StartDate]) < TypeConvert.ToDateTime(listDates[i][EndDate]))
{
retVal = true; //There are duplicates, hence return true
currentRow = i;
break;
}
else
{
//Do nothing as dates do not overlap
}
}
}
else
{
retVal = false; //As there is only 1 row, return false
}
if (retVal)
{
string message = "Dates Overlap";
//Display message
}
return retVal;
}
If you use SelectWithPrevious described here then you can write the following:
bool datesOverlap = table.Rows
.Cast<DataRow>()
.SelectWithPrevious((current, previous) => new
{
PreviousEndDate = (DateTime)previous["EndDate"],
StartDate = (DateTime)current["StartDate"]
})
.Any(x => x.StartDate < x.PreviousEndDate);

JTable + sorting column, but row is not sorted

I have created a JTable, the table contains 4 rows, and for a specific row, I have overridden the sorting and its working fine only on that column.
Status Scheduled Date Scheduled time Status
false 30/01/2012 02:00:00 Scheduled
false 29/01/2012 14:58:00 Scheduled
false 29/01/2012 15:50:00 Scheduled
For Scheduled Date, which I try to sort, it would sort, but the respecitve rows are not being updated.
Here is my code for sorting
public static void sortColumn(DefaultTableModel model, int colIndex,
boolean sortingOrder) {
Vector<?> data = model.getDataVector();
Object[] colData = new Object[model.getRowCount()];
SortedSet<Object> dataCollected = null;
List<Date> dateCollected;
boolean dateFlag = false;
dateCollected = new ArrayList<Date>();
// Copy the column data in an array
for (int i = 0; i < colData.length; i++) {
Object tempData = ((Vector<?>) data.get(i)).get(colIndex);
if ((colIndex == 1 || colIndex == 4)
&& tempData.toString().contains("/")) {
String[] _scheduledDate1 = ((String) tempData).split("/");
Calendar _cal1 = Calendar.getInstance();
_cal1.set(Integer.parseInt(_scheduledDate1[2]),
Integer.parseInt(_scheduledDate1[1]) - 1,
Integer.parseInt(_scheduledDate1[0]));
dateCollected.add(_cal1.getTime());
dateFlag = true;
} else {
colData[i] = ((Vector<?>) data.get(i)).get(colIndex);
}
}
// DateCompare compare = new DateCompare();
if (!dateFlag) {
dataCollected = new TreeSet<Object>();
dataCollected.add(colData);
dateFlag = false;
}
// Copy the sorted values back into the table model
if ((colIndex == 1 || colIndex == 4) && dateFlag) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sortOrder = !sortOrder;
if (sortOrder) {
Collections.sort(dateCollected);
} else {
Collections.sort(dateCollected, Collections.reverseOrder());
}
colData = dateCollected.toArray();
for (int i = 0; i < colData.length; i++) {
((Vector<Object>) data.get(i)).set(colIndex,
sdf.format(((Date) colData[i]).getTime()));
}
} else {
for (int i = 0; i < colData.length; i++) {
((Vector<Object>) data.get(i)).set(colIndex, colData[i]);
}
}
model.fireTableStructureChanged();
}
How to I get the entire row update accordingly?
I found the problem, my object against I was comparing was wrong, I've change the code for the same it all works fine.
I implemented QuickSort algorithm to sort the vector on the specific column I need.

Resources