Function to download anonymous type data into excel As far as I know, I cannot create function to accept anonymous type as parameter and I am reluctant to create new class to store those anonymous type because their difference is calculatedPrice.

How else can I remove\minimize repeated code in code below?

//.NET Framework 4
protected void btnDownload_Click(object sender, EventArgs e)
{
    var vwTransactionInfoItemProcesses = DAL.TransactionItemProcessHandler.GetVwTransactionInfoItemProcess(dropDownListTransactionStatus.SelectedValue.Trim(), BLL.SessionFacade.currentUserDeptCode.Trim(), true, txtSearchVendorCode.Text.Trim(), txtSearchCreateBy.Text.Trim(), txtSearchTransactionNumber.Text.Trim(), DateTime.Parse(txtCreateFrom.Text.Trim()), DateTime.Parse(txtCreateTo.Text.Trim()).Add(new TimeSpan(23, 59, 59)), txtSearchProductionOrder.Text.Trim(), txtSearchPartNumber.Text.Trim());

    if (BLL.UserRole.CheckUserRoleExist(BLL.SessionFacade.currentUserId, "purchaser"))
    {
        var vwTransactionInfoItemProcessesInDownloadFormat = vwTransactionInfoItemProcesses.Select(x =&gt; new
        {
            transactionNumber = x.transactionNumber,
            processName = x.processName,
            processOrder = x.processOrder,
            processQuantity = x.processQuantity,
            processUOM = x.processUOM,
            calculatedPrice = x.calculatedPrice
        }).ToList();
        DataTable transactionInfoItemProcessesInDownloadFormat = BLL.General.ConvertToDatatable(vwTransactionInfoItemProcessesInDownloadFormat);
        HSSFWorkbook workbook = BLL.General.GenerateNPOIExcelXLS(transactionInfoItemProcessesInDownloadFormat);
        ExportToExcel(workbook);
    }
    else
    {
        var vwTransactionInfoItemProcessesInDownloadFormat = vwTransactionInfoItemProcesses.Select(x =&gt; new
        {
            transactionNumber = x.transactionNumber,
            processName = x.processName,
            processOrder = x.processOrder,
            processQuantity = x.processQuantity,
            processUOM = x.processUOM
        }).ToList();
        DataTable transactionInfoItemProcessesInDownloadFormat = BLL.General.ConvertToDatatable(vwTransactionInfoItemProcessesInDownloadFormat);
        HSSFWorkbook workbook = BLL.General.GenerateNPOIExcelXLS(transactionInfoItemProcessesInDownloadFormat);
        ExportToExcel(workbook);
    }
}
Actually you can have anonymous types as parameters - called dynamics - but IMO it's not pretty (and probably rather slow):

static void ConvertToDatatable(IEnumerable&lt;dynamic&gt; input)
{

  foreach (var i in input)
  {
    Type type = i.GetType();
    if (type.GetProperty("Age") != null)
      Console.WriteLine($"{i.Name}: {i.Age}");
    else
      Console.WriteLine($"{i.Name}: No age");
  }
}

static void DynamicTest()
{
  bool isTrue = true;

  IEnumerable&lt;object&gt; data = null;

  if (isTrue)
  {
    data = Enumerable.Range(1, 5).Select(i =&gt; new { Name = "Name " + i, Age = i * 10 });
  }
  else
  {
    data = Enumerable.Range(1, 5).Select(i =&gt; new { Name = "Name " + i });
  }

  ConvertToDatatable(data);
}




I think I'd use a classical OOP approach:

class BaseClass
{
  public string Name;
}

class Sub1 : BaseClass
{
  public double Age;
}

class Sub2 : BaseClass
{
  public int Age;
}

static void ConvertToDatatable(IEnumerable&lt;BaseClass&gt; input)
{
  foreach (var i in input)
  {
    if (i is Sub1)
      Console.WriteLine($"{i.Name}: {(i as Sub1).Age}");
    else if (i is Sub2)
      Console.WriteLine($"{i.Name}: {(i as Sub2).Age}");
    else
      Console.WriteLine($"{i.Name}: No age");
  }
}

static void BaseClassTest()
{
  bool isTrue = true;

  IEnumerable&lt;BaseClass&gt; data = null;

  if (isTrue)
  {
    data = Enumerable.Range(1, 5).Select(i =&gt; new Sub1 { Name = "Name " + i, Age = i * 10 });
  }
  else
  {
    data = Enumerable.Range(1, 5).Select(i =&gt; new BaseClass { Name = "Name " + i });
  }

  ConvertToDatatable(data);
}




Beside that, I think your naming is quite remarkable and rather hard to read because of the length:

For instance it's hard to quickly distinguish between:

vwTransactionInfoItemProcessesInDownloadFormat 

transactionInfoItemProcessesInDownloadFormat 

vwTransactionInfoItemProcesses