SAREKA AVINASH
SAREKA AVINASH

Reputation: 479

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?

Upvotes: 0

Views: 957

Answers (1)

Vivek Nuna
Vivek Nuna

Reputation: 1

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.

Upvotes: 0

Related Questions