无法将类型'System.Collections.Generic.List <object>'隐式转换为'string'

时间:2019-05-09 01:55:37

标签: asp.net asp.net-mvc csv csvhelper

我的变量addresslist遇到错误。

Cannot implicitly convert type 'System.Collections.Generic.List<Object>' to 'string'

我一直在尝试使用来自CSV文件的数据填充嵌套List<Object>的模型,但遇到此错误。

这是我尝试填充嵌套List<Object>

的代码
public class FundTransferMap : ClassMap<FundTransfer>
    {
        public FundTransferMap()
        {
            Map(m => m.senderRefId);
            Map(m => m.sender).ConvertUsing(row =>
            {
                var senderlist = new List<Sender>
            {
                new Sender { name = row.GetField("senderName"),
                    address = Map(m => m.sender).ConvertUsing(row1 =>
                    {
                      var addresslist = new List<Address>
                    {
                            new Address { line1 = row1.GetField("senderAddrLine1"),
                            line2 = row1.GetField("senderAddrLine2"),
                            city = row1.GetField("senderCity"),
                            province = row1.GetField("senderProvince") }
                    };
                        return addresslist;
                    })
                },
            };
                return senderlist;
            });


        }
    }

这是我的模特

public class FundTransfer
    {
        public String senderRefId { get; set; }
        public List<Sender> sender { get; set; }
    }


public class Sender
    {
        public String name { get; set; }

        public List<Address> address { get; set; }

    }

 public class Address
    {
        public String line1 { get; set; }

        public String line2 { get; set; }

        public String city { get; set; }

        public String province { get; set; }
    }

1 个答案:

答案 0 :(得分:1)

您只需使用一次ConvertUsing

public class FundTransferMap : ClassMap<FundTransfer>
{
    public FundTransferMap()
    {
        Map(m => m.senderRefId);
        Map(m => m.sender).ConvertUsing(row =>
        {
            var senderlist = new List<Sender>
            {
                new Sender { name = row.GetField("senderName"),
                    address =  new List<Address>
                    {
                        new Address { line1 = row.GetField("senderAddrLine1"),
                        line2 = row.GetField("senderAddrLine2"),
                        city = row.GetField("senderCity"),
                        province = row.GetField("senderProvince") }
                    } 
                },
            };
            return senderlist;
        });
    }
}