如何从一个csv列到一些自定义代码进行自定义Map?

vdzxcuhz  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(102)

我有生成的实体类(如MyClass),我需要读取一个csv文件,然后返回MyClass的List。在所有实体上,我们都有一个readonly属性readOn。要设置CreatedOn,我们需要执行以下自定义代码:属性[“overriddencreatedon”] = DateTime.Now;
有没有可能使用CsvHelper来做这样的事情?:

public class MyClass // in real life, inherits from  Microsoft.Xrm.Sdk.Entity
{
    public Guid Id { get; set; }
    public DateTime CreatedOn { get;} // Readonly because generated class...
    public Dictionary<string, object> Attributes { get; set; }
}

private class MyClassMap : ClassMap<MyClass>
{
    public MyClassMap()
    {
        Map(f => f.Id).Name("Id");
        // Cannot do this : Map(f => f.CreatedOn).Name("CreatedOn");
        // Because CreatedOn is readonly
        // The goal is to store the value in the dictionary Attributes, not in the property CreatedOn because it is readonly
        // Instead, we need to do something like this:
        Map().XXX((row, x) => x.Attributes["overriddencreatedon"] = row.Row.GetField<DateTime>("CreatedOn"));
    }
}
mum43rcc

mum43rcc1#

这应该适用于Dictionary<string, object> Attributes

Map(f => f.Attributes).Convert(args =>
{
    return new Dictionary<string, object> {
        { "overriddencreatedon", args.Row.GetField<DateTime>("CreatedOn") }
    };
});

如果MyClass继承自Microsoft.Xrm.Sdk.Entity,那么它应该是AttributeCollection Attributes

Map(f => f.Attributes).Convert(args =>
{
    return new AttributeCollection {
        { "overriddencreatedon", args.Row.GetField<DateTime>("CreatedOn") }
    };
});

相关问题