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

问题描述 投票:0回答:1

尝试从地图中删除时间戳记时出现错误,并且想知道是否有任何可能的方法?

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()
c# datetime csvhelper
1个回答
0
投票

如错误消息所述,方法Map()期望仅在表达式中接收类的成员。您必须将日期格式移动到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());
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.