0

I have a C# project and looking for simple solution for map one class object data to list of another class object.

This is my input class

public class RatesInput
{
        public string Type1 { get; set; }
        public string Break1 { get; set; }
        public string Basic1 { get; set; }
        public string Rate1 { get; set; }

        public string Type2 { get; set; }
        public string Break2 { get; set; }
        public string Basic2 { get; set; }
        public string Rate2 { get; set; }

        public string Type3 { get; set; }
        public string Break3 { get; set; }
        public string Basic3 { get; set; }
        public string Rate3 { get; set; }

}

This is my another class structure

public class RateDetail 
{
            public string RateType { get; set; }
            public decimal Break { get; set; }
            public decimal Basic { get; set; }
            public decimal Rate { get; set; } 
}

it has a object like below. (For easiering the understanding, I use hardcoded values and actually values assign from a csv file)

RatesInput objInput = new RatesInput();
objInput.Type1 = "T";
objInput.Break1 = 100;
objInput.Basic1 = 50;
objInput.Rate1 = 0.08;
objInput.Type2 = "T";
objInput.Break2 = 200;
objInput.Basic2 = 50;
objInput.Rate2 = 0.07;
objInput.Type3 = "T";
objInput.Break3 = 500;
objInput.Basic3 = 50;
objInput.Rate3 = 0.06;

Then I need to assign values to "RateDetail" list object like below.

List<RateDetail> lstDetails =  new List<RateDetail>();

//START Looping using foreach or any looping mechanism

RateDetail obj = new RateDetail();
obj.RateType = //first iteration this should be assigned objInput.Type1, 2nd iteration objInput.Type2 etc....
obj.Break = //first iteration this should be assigned objInput.Break1 , 2nd iteration objInput.Break2 etc....
obj.Basic = //first iteration this should be assigned objInput.Basic1 , 2nd iteration objInput.Basic2 etc....
obj.Rate = //first iteration this should be assigned objInput.Rate1, 2nd iteration objInput.Rate2 etc....

lstDetails.Add(obj); //Add obj to the list
//END looping

Is there any way to convert "RatesInput" class data to "RateDetail" class like above method in C#? If yes, how to iterate data set?

weeraa
  • 1,123
  • 8
  • 23
  • 40
  • if the order matters, within the same for loop, use 3 lists to store objects with "1" values, "2" values and "3" values. After the for loop do a Union of all 3 lists – Matt.G Dec 06 '19 at 18:44
  • Thanks for the reply @Matt.G. Have you any clue to do looping through RatesInput class? Honestly, I have no idea to do it. Is it possible to drop a code sample? – weeraa Dec 06 '19 at 18:55
  • you should use [Automapper](https://github.com/AutoMapper/AutoMapper) – johnny 5 Dec 06 '19 at 19:23

2 Answers2

0

Try this:

    public class RatesList : IEnumerable<RateDetail>
    {
        public RatesList(IEnumerable<RatesInput> ratesInputList)
        {
            RatesInputList = ratesInputList;
        }

        private readonly IEnumerable<RatesInput> RatesInputList;
        public IEnumerator<RateDetail> GetEnumerator()
        {
            foreach (var ratesInput in RatesInputList)
            {
                yield return new RateDetail
                {
                    RateType = ratesInput.Type1,
                    Break = Convert.ToDecimal(ratesInput.Break1, new CultureInfo("en-US")),
                    Basic = Convert.ToDecimal(ratesInput.Basic1, new CultureInfo("en-US")),
                    Rate = Convert.ToDecimal(ratesInput.Rate1, new CultureInfo("en-US"))
                };
                yield return new RateDetail
                {
                    RateType = ratesInput.Type2,
                    Break = Convert.ToDecimal(ratesInput.Break2),
                    Basic = Convert.ToDecimal(ratesInput.Basic2),
                    Rate = Convert.ToDecimal(ratesInput.Rate2, new CultureInfo("en-US"))
                };

                yield return new RateDetail
                {
                    RateType = ratesInput.Type3,
                    Break = Convert.ToDecimal(ratesInput.Break3),
                    Basic = Convert.ToDecimal(ratesInput.Basic3),
                    Rate = Convert.ToDecimal(ratesInput.Rate3, new CultureInfo("en-US"))
                };

            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }

And use:

var list = new RatesList(new List<RatesInput>() { objInput });

foreach (var item in list)
{
    Console.WriteLine(item.Basic);
}
Basil Kosovan
  • 888
  • 1
  • 7
  • 30
0

You can use Reflection to get the properties info like this:

        var props = objInput.GetType().GetProperties();

        var types = props.Where(x => x.Name.StartsWith("Type"))
            .Select(x => x.GetValue(objInput)).ToList();
        var breaks = props.Where(x => x.Name.StartsWith("Break"))
            .Select(x => x.GetValue(objInput)).ToList();
        var basics = props.Where(x => x.Name.StartsWith("Basic"))
            .Select(x => x.GetValue(objInput)).ToList();
        var rates = props.Where(x => x.Name.StartsWith("Rate"))
            .Select(x => x.GetValue(objInput)).ToList();

        List<RateDetail> lstDetails = new List<RateDetail>();

        for (int i = 0; i < types.Count; i++)
        {
            lstDetails.Add(new RateDetail
            {
                RateType = types[i].ToString(),
                Break = Convert.ToDecimal(breaks[i]),
                Basic = Convert.ToDecimal(basics[i]),
                Rate = Convert.ToDecimal(rates[i])
            });
        }
Hadi Samadzad
  • 1,480
  • 2
  • 13
  • 22