AngelicCore
AngelicCore

Reputation: 1453

Add property to list of a class

I have a List of a OrdersInfo I inherit OrdersInfo into YearlyResourceReport and add 12 properties - months (public string Jan {get;set;}) etc

Now i create a list of the new class (So [OldProperties] + [12Month])

How can i merge the two lists together?

class YearlyResourceReport : OrdersInfo
{
    public string Jan { get; set; }
    public string Feb { get; set; }
    public string Mar { get; set; }
    public string Apr { get; set; }
    public string Jun { get; set; }
    public string Jul { get; set; }
    public string Aug { get; set; }
    public string Sep { get; set; }
    public string Oct { get; set; }
    public string Nov { get; set; }
    public string Dec { get; set; }

}

Addition:

List<OrdersInfo> ordersList = 
WorkOrderEntity.GetYearlyOrders(year, loggedInUser, customerIds, sortBy).ToList();

List<YearlyResourceReport> newList;

I wish to either add properties to first list(without having to create a second list at all) Or just as a last measure merge both lists.

Upvotes: 1

Views: 5750

Answers (2)

Santona
Santona

Reputation: 31

If i do understand your question correctly you would like to do it something like this.

// Your class definitions
public class YearlyResourceReport
{
    public YearlyResourceReport()
    {
        this.MonthlyResourceReports = new List<MonthlyOrderInfo>();
    }

    public List<MonthlyOrderInfo> MonthlyResourceReports { get; set; }
}

public class MonthlyOrderInfo
{
    public string Month { get; set; }
    public int MonthNumber { get; set; }
    public OrdersInfo OrdersInfo { get; set; }
}

public class OrdersInfo
{
    public int Id { get; set; }
    public string description { get; set; }
    public int TotalOrders { get; set; }
    public double TotalRevenue { get; set; }
}

You could create the containing YearlyResourceReport class as follows:

 public YearlyResourceReport GetYearlyOrders()
 {
    YearlyResourceReport yrr = new YearlyResourceReport();
    for(int i = 1; i <= 12; i++)
    {
        yrr.MonthlyResourceReports.Add(new MonthlyOrderInfo {
        moi.MonthNumber = i,
        moi.OrdersInfo = WorkOrderEntity.GetMonthlyOrders(year, i, loggedInUser, customerIds, sortBy).ToList()
        });
    }
    return result;
 }

Upvotes: 1

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

This should work as an example to get you going down the right path:

class A
{
    public string PropertyA { get; set; }

    public override string ToString() { return this.PropertyA; }
}

class B : A
{
    public string PropertyB { get; set; }

    public override string ToString() { return string.Format("{0} - {1}", this.PropertyA, this.PropertyB); }
}

var aList = new List<A>();
var bList = new List<B>();

for (int i = 0; i < 10; i++)
{
    aList.Add(new A() { PropertyA = string.Format("A - {0}", i) });
    bList.Add(new B() { PropertyA = string.Format("B::A - {0}", i), PropertyB = string.Format("B::B - {0}", i) });
}

// now list the current state of the two lists
for (int i = 0; i < 10; i++)
{
    Console.WriteLine(aList[i]);
    Console.WriteLine(bList[i]);
}

Console.WriteLine();
Console.WriteLine();

// now merge the lists and print that result
var newList = bList.Concat(aList.Select(a => new B() { PropertyA = a.PropertyA, PropertyB = "Created by system." }));
foreach (var item in newList)
{
    Console.WriteLine(item);
}

In the above example I have a class A and B and B inherits from A and adds a property. I then build a listing of both and write them out to the Console. After doing that we then merge the two lists, creating B's out of the A's because a singular generic list must have the same type. You could conceivably use something like an ArrayList instead of a generic list and house two different types - but I don't think that's what you're looking for.

Once merging the types we write out the results of that merge to the Console as well and you see the two lists merged.

If your requirements are a little different, this will get you more than 90% of the way, because honestly your question didn't contain very much information so you left us to kind of assume some things.

NOTE: I used scriptcs to compile, run, and prove this example so that's why it's not as structured.

Upvotes: 4

Related Questions