none
Class table to Account table copying RRS feed

  • Question

  • I'm using Linq to SQL and have a situation where I have a table of classes which contain default settings for account table entries. 

    I want to create a new account entity, iterate through the columns of the class table record for the class that this account will belong to and copy the class values from the class record into the new account record.

    With System.Data objects, this was no problem.  I would create a new DataRow for the account and then access the class record and iterate through the columns of that table, get the column name and search the columns of the account table for a matching column name.  If found I made the assignment.

    That code was something like:

    DataRow rw = dataset.Tables["accounttable"].NewRow();

    DataRow[] classes = dataset.Tables["classtable"].Select("Class = 'thisclass');

    string classfield = "";

    foreach (DataColumn c in dataset.Tables["classtable"].Columns)

    {

        classfield = c.ColumnName.Trim();

        foreach (DataColumn a in dataset.Tables["accountable"].Columns)

        {

            if (a.ColumnName.Trim() == classfield)

                rw[classfield] = classes[0][classfield];

        }

    }

    How can I do this with LINQ to SQL? 


    JLL_NV

    Tuesday, December 30, 2014 3:45 PM

Answers

  • Hello JLL_NV,

    >> How can I do this with LINQ to SQL?

    You could do it with refection in .NET as below:

    using (DataClasses1DataContext db = new DataClasses1DataContext())
    
                    {
    
                        Order order = db.Orders.FirstOrDefault();
    
                        OrderDetail od = new OrderDetail();
    
    
                        var orderProperties = order.GetType().GetProperties();
    
                        var odProperties = od.GetType().GetProperties();
    
    
                        foreach (PropertyInfo p1 in orderProperties)
    
                        {
    
                            foreach (PropertyInfo p2 in odProperties)
    
                            {
    
                                if (p2.Name == p1.Name)
    
                                {
    
                                    p2.SetValue(od, p1.GetValue(order));
    
                                }
    
                            }
    
                        }
    
    
                    }
    

    You could assume the “Order” as class and “OrderDetail” to be your account table.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Jll_NV Wednesday, December 31, 2014 6:27 PM
    Wednesday, December 31, 2014 8:06 AM
    Moderator

All replies

  • Hello JLL_NV,

    >> How can I do this with LINQ to SQL?

    You could do it with refection in .NET as below:

    using (DataClasses1DataContext db = new DataClasses1DataContext())
    
                    {
    
                        Order order = db.Orders.FirstOrDefault();
    
                        OrderDetail od = new OrderDetail();
    
    
                        var orderProperties = order.GetType().GetProperties();
    
                        var odProperties = od.GetType().GetProperties();
    
    
                        foreach (PropertyInfo p1 in orderProperties)
    
                        {
    
                            foreach (PropertyInfo p2 in odProperties)
    
                            {
    
                                if (p2.Name == p1.Name)
    
                                {
    
                                    p2.SetValue(od, p1.GetValue(order));
    
                                }
    
                            }
    
                        }
    
    
                    }
    

    You could assume the “Order” as class and “OrderDetail” to be your account table.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Jll_NV Wednesday, December 31, 2014 6:27 PM
    Wednesday, December 31, 2014 8:06 AM
    Moderator
  • Thank you -- I will give that a try.

    Happy New Years


    JLL_NV

    Wednesday, December 31, 2014 6:27 PM