-1

I have some code that's main purpose is to combine multiple like lists into one master list to return with the View.

    ActivityAuditDetails searchParams = new ActivityAuditDetails();
    ActivityAuditDetails finalResults = new ActivityAuditDetails();
    List<string> finalChangedColumns = new List<string>();
    List<string> finalOldValues = new List<string>();
    List<string> finalNewValues = new List<string>();
    string finalAuditAction = string.Empty;

    List<int> auditKeys = AuditIdentityId.Split(',').Select(int.Parse).ToList();
    string url = "/Audit/GetActivityAuditDetails";

    try
    {
        foreach (int auditKey in auditKeys)
        {
            searchParams.AuditIdentityId = auditKey;
            ActivityAuditDetails result =  // SOME METHOD THAT RETURNS RESULTS AS IT SHOULD;

            finalChangedColumns.Concat(result.ChangedColumns);
            finalAuditAction = result.AuditAction;
            finalOldValues.Concat(result.OldValues);
            finalNewValues.Concat(result.NewValues);
        }

        finalResults.ChangedColumns = finalChangedColumns;
        finalResults.AuditAction = finalAuditAction;
        finalResults.OldValues = finalOldValues;
        finalResults.NewValues = finalNewValues;

    }
    catch (Exception e)
    {
        e.ToLog();
    }
    return View(finalResults);

I can see that the result object is populated as it should be in the debugger. I thought the Concat method would work to combine the lists, but my final values in the foreach loop never get update\incremented ( the list count remains zero ).

Is there another way to accomplish this, or am I having a morning brain fart? My question was not about the differences, as I was aware of them. I just had a momentary lapse of reason.

Neo
  • 3,309
  • 7
  • 35
  • 44
  • 2
    I think that `Concat` returns the new value,, but you don't do anything with it – devqon Jul 10 '17 at 13:27
  • What do you have to do to get an upvote? ( Guess I should be glad I didn't get a downvote ) – Neo Jul 10 '17 at 13:38
  • @mjwills Not really a duplicate, I knew the difference, I just made a mistake as to the implementation. :-) – Neo Jul 10 '17 at 13:40
  • What the hell? A downvote for this question, no wonder many hate using this site. SMH – Neo Jul 11 '17 at 16:54

2 Answers2

2

You likely want to use AddRange rather than Concat.

The former adds the data directly to the List. The latter concatenates data into a new IEnumerable.

But because you are not assigning the result of Concat to anything (i.e. var g = finalChangedColumns.Concat(result.ChangedColumns);) your Concat calls do effectively nothing.

mjwills
  • 23,389
  • 6
  • 40
  • 63
1

List<T>.AddRange(IEnumerable<T> collection) (link to info) probably does what you're looking for, right?

Adds the elements of the specified collection to the end of the List.

From documentation:

string[] input = { "Brachiosaurus", 
                   "Amargasaurus", 
                   "Mamenchisaurus" };

List<string> dinosaurs = new List<string>();
dinosaurs.AddRange(input);
//The list now contains all dinosaurs from the input string array
rickvdbosch
  • 14,105
  • 2
  • 40
  • 53