从 C# 中的地图中删除时间戳

Remove the timestamp from a map in C#

我在尝试从地图中删除时间戳时遇到错误,想知道是否有任何方法可以做到这一点?

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

我遇到错误:

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()

如错误消息所述,方法 Map() 预计只会在表达式中接收 class 的成员。您必须将日期格式移动到 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());
    }
}