Remove the timestamp from a map in C#

0

I am getting an error when attempting to to remove the timestamp from a Map and was wondering if there were any way that this would be possible?

AutoMap();
Map(m => m.BeginDate.Value.ToShortDateString()).Name("Delivery Begin Date").Index(0);

I am getting the error:

System.InvalidOperationException
  HResult=0x80131509
  Message=No members were found in expression '{expression}'.
  Source=CsvHelper
  StackTrace:
   at CsvHelper.Configuration.ClassMap`1.Map[TMember](Expression`1 expression, Boolean useExistingMap)
   at Cgb.Grain.Customer.Service.Csv.BidsheetCsvMap..ctor() in C:\Users\larkb\Source\Repos\GrainCustomer\GrainCustomerService\Csv\BidsheetCsvMap.cs:line 33
   at Cgb.Grain.Customer.Service.CsvExport.<>c.<.ctor>b__1_5() in C:\Users\larkb\Source\Repos\GrainCustomer\GrainCustomerService\Csv\CsvExport.cs:line 27
   at Cgb.Grain.Customer.Service.CsvExport.WriteCsvToMemory[T](IEnumerable`1 items) in C:\Users\larkb\Source\Repos\GrainCustomer\GrainCustomerService\Csv\CsvExport.cs:line 40
   at GrainCustomerService.Controllers.BidsheetController.GetBidsheetsAsCsv(Nullable`1 accountId) in C:\Users\larkb\Source\Repos\GrainCustomer\GrainCustomerService\Controllers\BidsheetController.cs:line 118
   at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.MoveNext()
c#
datetime
csvhelper
asked on Stack Overflow Jan 10, 2020 by simplest • edited Jan 10, 2020 by (unknown user)

1 Answer

1

As the error message says, the method Map() expects to only receive a member of your class in the expression. You have to move the formatting of the date to ConvertUsing().

public class Program
{
    public static void Main(string[] args)
    {
        var records = new List<Foo> { new Foo { Id = 1, BeginDate = DateTime.Now } };

        using(var csv = new CsvWriter(Console.Out))
        {
            csv.Configuration.RegisterClassMap<FooMap>();
            csv.WriteRecords(records);
        }

        Console.ReadLine();
    }
}

public class Foo
{
    public int Id { get; set; }
    public DateTime BeginDate { get; set; }
}

public class FooMap : ClassMap<Foo>
{
    public FooMap()
    {
        AutoMap();
        Map(m => m.BeginDate).Name("Delivery Begin Date").Index(0).ConvertUsing(x => x.BeginDate.ToShortDateString());
    }
}
answered on Stack Overflow Jan 10, 2020 by David Specht • edited Jan 10, 2020 by David Specht

User contributions licensed under CC BY-SA 3.0