简体   繁体   English

从集合映射到数组

[英]Mapping from a collection to an array

Hi I looked and only saw examples from arrays to list\\collection but not the other way around. 嗨,我看了看,只看到了从数组到list \\ collection的示例,但没有看到相反的例子。

I have a collection with an attribute AccIndex that I want to map to an array with an attribue named AccIndex . 我有一个具有属性AccIndex的集合,我想映射到具有名为AccIndex的属性的数组。

I tried the following: 我尝试了以下方法:

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

This gives me null , and with the toArray I feel it will only make everything slower (toArray and than breaking it up again). 这给了我null ,并且使用toArray我觉得它只会使一切变慢(toArray而不是再次分解)。

Isn't there a simple way to map between the two? 在这两者之间映射是否简单? The only difference is that one is a collection and the other array but somehow I find nothing on google. 唯一的区别是,一个是集合,另一个是数组,但不知何故我在Google上一无所获。

Some more stuff I tried that always resulted with many nulls: 我尝试过的其他东西总是会导致很多null:

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

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

I was asked to show how the collection and array look like so: 我被要求展示集合和数组的样子:

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; }
}

And an array of these: 以及这些数组:

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

Thanks 谢谢

Ok I fixed my problem. 好吧,我解决了我的问题。 My problem was I didn't use the mapper correctly. 我的问题是我没有正确使用映射器。 On the "CreateMapper" U need to put only the core items, and on the "Mapper.Map" u need to put only the generic collection of them. 在“ CreateMapper”上,U只需要放置核心项,在“ Mapper.Map”上,U只需要放置它们的通用集合。 In my case: 就我而言:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM