Wednesday, June 8, 2011

demo project convert Datatable to LIST


You can find the project here

Explanation of both conversions from DataTable to List using Reflection and LINQ are already explained in the previous posts.

Friday, May 20, 2011

Convert datatable into list using reflection

Using reflection we can convert the datatable into strongly typed collection. I'll prefer list as a collection.

Let's suppose I have a class of Emp and I have put two fields in it. 
class Emp{ string Id {get; set;} string Name{get; set;}}
Now, to convert datatable into any class, Ofcourse we'll have to use Generic template that will find the type of the class. 
We are creating a class that will convert Datatable into strongly typed list. 

This is the declaration of a class where we will refer any business class as a 'T' and that 'T' type should have a constructor with no arguments. We are ensuring in the where. 
public class DatatableToListMapper : where T : new()
We will create a method ConvertDataTableIntoList where Datatable will be passed as an argument and this method will return List of type T.
Here's the signature of the method.
List ConvertDataTableIntoList(DataTable dt)
// create list of type T.
List objT = new List();
Now we will fetch every row from the DataTable.
foreach(DataRow dr in dt.Rows)
Now we will get the properties of the T class using the method typeof(T).GetProperties() and will retrieve every property using loop. Create an object of type T.

e.g. 
T obj = new T();
foreach(var property in typeof(T).GetProperties()){
property.SetValue(obj, dr[property.Name], null); /// Setting the property value in the object obj.
}
//Add this object in a 
objT.Add(obj);

At last return the object of list after conversion. I'll share the code in the next post where the mapping can be user defined using a Dictionary collection.


return objT;


Thursday, May 19, 2011

Convert Datatable into collection using LINQ

We can create the anonymous type from the data table but If we are interested to convert data table into strongly typed collection like List of Emp type from the datatable. Below is the code snippet.

    public class Emp
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

                DataTable dt = new DataTable();
                dt.Columns.Add("EmpId");
                dt.Columns.Add("EmpName");
                dt.Rows.Add("1", "Sohail");
                dt.Rows.Add("2", "Yasir");
                dt.Rows.Add("3", "Shahid");
Convert data type into AsEnumerable and then select the rows from the table and put one by one field in the emp object.
             List emp = new List();
             var result = from p in dt.AsEnumerable()
             select new Emp
             {
                     Id = int.Parse(p[0].ToString()),
                     Name = p[1].ToString()
             };

          After all, convert the result into List of Emp type.
           emp = result.ToList();       
           foreach (var row in emp)
           Console.WriteLine("{0}, {1}", row.Id, row.Name);

Friday, April 29, 2011

Defaulting Dimension in AX

What is defaulting dimension means?

Defaulting dimension means to provide the default values to the generated ledger dimension account from the originating account if the dimension values are empty for the generated ledger dimension.

For example:

If I give Mainaccount-Fund as a dimension structure.

LedgerDimensionOriginatingAccount = 10200 - 100
LedgerDimensionGeneratedAccount = 10200 - --(empty)

After processing defaulting dimension, a new ledger dimension will be generated and 10200-100 will be a new ledger dimension. I will share the code in the next post how to the dimension defaulting can be done.




Sunday, April 24, 2011

Convert object into anonymous type C#

Firstly I want to tell you that to use anonymous types isn't best practice. I was thinking if we can parse anonymous type into object then object should be parsed into anonymous type too.

here's an example.
lets suppose, we create a list of object type and we add some items in it of a anonymous type.

List obj = new List();

obj.Add((object)new {Id=1, Name="S"});
obj.Add((object)new {Id=2, Name="N"});
obj.Add((object)new {Id=3, Name="NS"});



 In the above list, I have added three objects. How can I retrieve the Id and Name attribute. There are two possibilities one we can create a strongly typed class but that will waste the purpose of a Anonymous type and the other one is to use using Generics or Templates. I will create a small method that will convert object into Anonymous type and will be available for querying the data in the code... 
public static T ConvertObjectIntoAnonymousType(T object, T typeOfObject) {         
return (T)object; 
}
 How we will use this function. 
object o = obj[0]; 



var anonymousObject = ConvertObjectIntoAnonymousType(o, new {Id=0, Name=""}); 


Its been converted. What we did? We created a anonymous type in the second argument of the same as we have the list object type. This type will be hold in T of the function ConvertObjectIntoAnonymousType and will be able to cast object into type T that is anonymous type. Using this technique we can save the time to create classes. I used this technique when I was using deserializing the dataset's XML for querying data. Hope this would help to everyone.

Thursday, April 14, 2011

Dimension framework in Microsoft Dynamics AX 2012

AX has introduced dimension framework. A detailed white paper is available on the below link.

Accounts and Financial dimension framework.

Let me know in case of any query regarding accounts dimension or financial dimensions.

The Intelligent Data Management in MS Dynamics AX

Find the attached link of a presentation that is delivered by Tao Wang Principle Development Manager of AX Performance.
Intelligent Data Management in MS Dynamics AX presentation link.

Wednesday, March 2, 2011

Configure and update OLAP cubes



Note: Make sure you have configuration/license keys configured, otherwise the dimensions that have no access of the field of OLTP will be removed.


Step # 1:


OLTP database synchronization of OLAP tables:


    The following procedure is used to update the BI tables that are purely related to BI cubes and used as a   dimension in the BI cubes. Before executing the procedure you need to perform some pre-requistes listed below:
1 - Go into Administration tab> Setup> Business Analysis> OLAP and click on OLAP Administration.
2 - A form will be opened.
3 - Go onto the Advanced tab of the form and mark the checkbox 'Update BI Data'.
4 - Press the button 'Update database'
You will observe that the BI related tables are filled and now these tables can be used for BI analysis after processing.


Step # 2:


OLAP database synchronization with OLTP database:

We know that OLAP has a different schema and is mapped directly to the OLTP tables in order to provide the records in OLAP cube's dimensions and measures.
Same form will be used to synchronize OLAP database. The only difference is that  here we need to provide the datasource and the server where the OLAP database schema exist. For  this task follow the steps below:
1 - Go onto OLAP Administration form.
2 - A form will be opened.
3 - Go onto the OLAP servers tab. Select the server (instance)
4 - Go onto the OLAP databases tab. Select the OLAP database.
5 - Now go onto the advanced tab, click on the checkbox Synchronise OLAP database with OLTP schema.
6 - Click update database button.
This will update the OLAP schema. Same procedure will be applied if you want database changes to be shown in OLAP schema after inserting any records in OLAP tables like CustTrans (When creating FTI), PurchTrans, LedgerTrans etc.

Soon, I will talk about how to update the OLAP database incase if you have any tables and want to use as a dimension and measures in the OLAP cubes..