gpt4 book ai didi

c# - 从集合到数组的映射

转载 作者:太空宇宙 更新时间:2023-11-03 15:38:42 25 4
gpt4 key购买 nike

您好,我查看并只看到了从数组到列表\集合的示例,但没有看到相反的示例。

我有一个集合,其属性 AccIndex 我想映射到具有名为 AccIndex 的属性的数组。

我尝试了以下方法:

Mapper.CreateMap<AccountCollection, AcctArray[]>().
ForMember(d=> d.AccIndex,
o=>MapFrom(s => s.AcctList.Select(c => c.AccIndex).ToArray()));

这给了我 null,并且使用 toArray 我觉得它只会让一切变慢(toArray 而不是再次分解它)。

是否有一种简单的方法可以在两者之间进行映射?唯一的区别是一个是集合而另一个是数组,但不知何故我在谷歌上找不到任何东西。

我尝试了一些总是导致很多空值的东西:

Mapper.CreateMap<AccountCollection,
AcctArray>();
Mapper.Map(AcctIndexesList, newIndexesArray);

Mapper.CreateMap<AccountCollection,
AcctArray[]>();
Mapper.Map(AcctIndexesList, newIndexesArray2);

我被要求展示集合和数组的样子:

public class AccountCollection: IList, ICollection, IList<AcctIndexes>,     
ICollection<AcctIndexes>, IEnumerable<AcctIndexes>, IEnumerable {

public AcctIndexesCollection();
public virtual int Count { get; }
public int Rows { get; set; }
public virtual AcctIndexes this[int index] { get; set; }
public virtual int Add(AcctIndexes value);
...
}

public class AcctIndexes : IFormattable
{
public bool AccIndexSpecified;
public AcctIndexes();

public int AccIndex { get; set; }
}

以及这些的数组:

public partial class AcctArray {
private int accIndexField;
public int AccIndex
{
get {
return this.accIndexField;
}
set {
this.accIndexField = value;
}
}
}

谢谢

最佳答案

好的,我解决了我的问题。我的问题是我没有正确使用映射器。在“CreateMapper”上,您只需放置核心项目,而在“Mapper.Map”上,您只需放置它们的通用集合。就我而言:

Mapper.CreateMap<AcctIndexes, AcctIndexes>();
Mapper.Map<ICollection<AcctIndexes>, AcctIndexes[]>();

关于c# - 从集合到数组的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30965649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com