简体   繁体   中英

add values from one list to another based on a condition

I have two list

List<string[]> List1;
List<string[]> List2;

List1 looks like this:

    SECTION    BEGINNING Value  Ending Value
    section2    31507976.71          0
    section6    31643256.16          0
    section8    32297021.88          0
    section14   31643256.16          0

List2:

    SECTION     Ending Value
    section2    31406327.47
    section8    33863875.36
    section10   32674862.89

I want to add items from list2 to list1 based on the values from first column of each lists. List1 should looks like this:

 SECTION    BEGINNING Value     Ending Value
section2    31507976.71          31406327.47
section6    31643256.16              0
section8    32297021.88          33863875.36
section14   31643256.16              0

Here is my code:

public static void getList()
{
    var list1 = new[]
    {
    new {Section = "section2", BeginningValue = 31507976.71, EndingValue=0},
    new {Section = "section6", BeginningValue = 31643256.16, EndingValue=0},
    new {Section = "section8", BeginningValue = 32297021.88, EndingValue=0},
    new {Section = "section14", BeginningValue = 31643256.16, EndingValue=0},
    };
    var list2 = new[]
    {
        new {Section = "section2",  EndingValue=31406327.47},
        new {Section = "section8",  EndingValue=33863875.36},
        new {Section = "section10", EndingValue=32674862.89},
    };
    var result = list1.Concat(list2).OrderByDescending(x => x.EndingValue).GroupBy(g => x.Section).ToList();

    foreach (var item in result)
        Console.WriteLine(item.Section + "-" + item.BeginningValue + "-" + item.EndingValue);
}

Simple linq approach

var result = list1.Select(x => new 
            { 
            Section = x.Section, 
            BeginningValue = x.BeginningValue, 
            EndingValue = list2.Any(y => y.Section == x.Section) ? 
                          list2.First(y => y.Section == x.Section).EndingValue : 
                          0 
            }).ToList();

Please modify your getList function with this one:

public static void getList()
{
    var list1 = new[] {
                        new {Section = "section2", BeginningValue = 31507976.71, EndingValue=0},
                        new {Section = "section6", BeginningValue = 31643256.16, EndingValue=0},
                        new {Section = "section8", BeginningValue = 32297021.88, EndingValue=0},
                        new {Section = "section14", BeginningValue = 31643256.16, EndingValue=0},
                    };
    var list2 = new[]{
                        new {Section = "section2",  EndingValue=31406327.47},
                        new {Section = "section8",  EndingValue=33863875.36},
                        new {Section = "section10", EndingValue=32674862.89},
                    };

    var result = from Item1 in list1
                 join Item2 in list2
                 on Item1.Section equals Item2.Section into ps
                 from p in ps.DefaultIfEmpty()
                 select new
                 {
                     Section = Item1.Section,
                     BeginningValue = Item1.BeginningValue,
                     EndingValue = (p != null ? p.EndingValue : Item1.EndingValue)
                 };

    foreach (var item in result)
        Console.WriteLine(item.Section + "-" + item.BeginningValue + "-" + item.EndingValue);
}

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