Search code examples
c#devexpressaspxgridviewgeneric-list

Copying a generic list to another, with different data types takes too long (C#)


I have a List<CallLog>, where CallLog has the following properties:

public string SomeProperty1 { get; set; }
public string SomeProperty2 { get; set; }
public string SomePropertyN { get; set; }
public DateTime Date { get; set; }
public DateTime CallEndTime { get; set; }    

I need to fill a (devexpress) grid data source with this list. The grid has the following columns:

Date | SomeProperty1 | SomeProperty2 | ... | SomePropertyN | CallStartTime | CallEndTime

In fact, I am using the Date property in 2 columns, in the first column (Date), I use only the date part and in the other column (CallStartTime), I use the time part only.

Unfortunately, gridxview fails to perform proper grouping when I simply ask it to show different formats of my Date property in 2 columns. So, I decided to create a customized class and copy the original list into a new list (List<CustomizedCallLog>) and pass the new list as the grid data source. Below is how I am doing this. But it takes too long. Is there any fast way to copy the list?

My customized call log class:

Public Class CustomizedCalLog
{
    public DateTime Date { get; set; }
    public string SomeProperty1 { get; set; }
    public string SomeProperty2 { get; set; }
    public string SomePropertyN { get; set; }
    public string CallStartTime { get; set; }
    public string CallEndTime { get; set; }

    public CustomizedCallLog(CallLog log)
    {
        this.Date = log.Date;
        this.SomeProperty1 = log.SomeProperty1 ;
        this.SomeProperty2 = log.SomeProperty2 ;
        this.SomePropertyN = log.SomePropertyN ;
        this.CallStartTime = log.Date.ToString("H:mm");
        this.CallEndTime = log.CallEndTime.ToString("H:mm");           
    }        
}

Setting the grid:

private void SetGrid()
{
    var logs = someBusiness.GetLogs();
    List<CustomizedCallLog> dataSource = new List<CustomizedCallLog>();
    foreach (var log in logs)
    {
        dataSource.Add(new CustomizedCallLog(log));
    }

    grid.ForceDataRowType(typeof(CustomizedCallLog));
    grid.DataSource = dataSource;
    grid.DataBind();
}

Solution

  • My issue is resolved using the comment by @Yuval Itzchakov.

    I could not add the new properties to my existing class since the existing class was auto-generated (with some .tt file).

    So, I left the code as it is, and improved the this.SomePropertyX = log.SomePropertyX bit of code which was making a bottleneck.