简体   繁体   中英

How to increment an integer property for every class inside List in c#

I am having a class as below:

public class DBEmployee
{
  public Guid EmpId {get;set;}
  public string Name {get;set;}
  public int IncrementNo {get;set;}
}

public class C#Employee
{
  public Guid EmpId {get;set;}
  public string Name {get;set;}
  public int IncrementNo {get;set;}
}

Consider I got List of Employees from database and bind it to the variable listOfEmployees as below:


                var listOfEmployees = new List<DBEmployee>
                                            {
                                                new DBEmployee {
                                                                EmmId = "e31d712a-7d5c-4b1c-99a8-306f9aebbfa0",
                                                                Name = "AAA",
                                                                IncrementNo = 0
                                                             },
                                                new DBEmployee {
                                                                EmmId = "87c4feed-20e7-42b2-8a95-8acb0743fdfe",
                                                                Name = "BBB",
                                                                IncrementNo = 0
                                                             },
                                                new DBEmployee {
                                                                EmmId = "90cdd219-8796-4d51-bfe1-add8515315c5",
                                                                Name = "CCC",
                                                                IncrementNo = 0
                                                             },
                                            }

Now I am using AutoMapper to map DBEmployee to C#Employee as below:

var config = new MapperConfiguration(cfg => cfg.CreateMap<DBEmployee, C#Employee>()
                                              .AfterMap((src, dest) => dest.IncrementNo= dest.IncrementNo+ 1)
                                              );
var mapper = new Mapper(config);
var c#Emps = mapper.Map<List<C#Employee>>(listOfEmployees);

I tried in above format but it's always getting IncrementNo as 1 for every Employee.

Now I want the IncrementNo property to be increment by +1 for every employee object such as Employee Name AAA should contain IncrementNo=1 and Employee Name BBB should contain IncrementNo=1 and Employee Name CCC should contain IncrementNo=3.

I can acheieve this by using Foreach loop as below:

var lR = 0;
c#Emps.ForEach(x =>
                {
                    x.IncrementNo= lR + 1;
                    lR++;
                });

but i just want to try in a better way mostly while configuring AutoMapper itself rather that having more lines of code using ForEach

Could someone help me with this?

Have you tried this?

Mapper.CreateMap<DBEmployee, C#Employee>()
.AfterMap((src, dest) => 
{
    int incrementNo = 1;
    foreach (var employee in listOfEmployees)
    {
        employee.IncrementNo = incrementNo++;
    }
});

Sorry please ignore typing mistakes as I have not compiled the code. But conceptually it should be similar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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