1

For Instance

List<BursaryPaymentSplitting> splitAccount = new List<BursaryPaymentSplitting>();

foreach ( var lineItem in splitAccount)
{
    var lineItems = new RemitaSplit { 
        lineItemsId = lineItem.BursaryPaymentSplittingId.ToString(), 
        beneficiaryName = lineItem.AccountName, 
        beneficiaryAccount = lineItem.AccountNumber, 
        bankCode = lineItem.BankCode, 
        beneficiaryAmount = lineItem.Amount.ToString(), 
        deductFeeFrom = lineItem.DeductFeeFrom.ToString() 
    };
}

How do i make use of the variable lineItems outside the foreach function block

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • 2
    You can't. If you want to use the new objects, use `Select` instead and loop over them – Panagiotis Kanavos Oct 22 '19 at 11:36
  • You can only use the variable if you declare it outside the foreach. Such as: `RemitaSplit lineItems = null;`. After the foreach check for `null` to see if the variable now holds a (valid) value. – Stefan Oct 22 '19 at 11:38
  • `lineItems` is a single `RemitaSplit` instance. What do you want to do with it? It doesn't have any meaning outside the loop anyway - even if you put it outside the loop, it will only hold the last loop value once the loop finishes. – Panagiotis Kanavos Oct 22 '19 at 11:41
  • if you made a class or structure you could store the line items details in a list and access the processed results – BugFinder Oct 22 '19 at 11:49

2 Answers2

2

Declare the variable in the scope that you need it. For example:

RemitaSplit lineItems;
foreach (var lineItem in splitAccount)
{
    lineItems = new RemitaSplit { /.../ };
}
// Here you can access the lineItems variable.
// Though it *might* be NULL, your code should account for that.

Though what's strange here is that you're overwriting the same variable over and over. Why? Did you mean to have a collection of objects instead of just one object? Something like this?:

var lineItems = new List<RemitaSplit>();
foreach (var lineItem in splitAccount)
{
    lineItems.Add(new RemitaSplit { /.../ });
}
// Here you can access the lineItems variable.
// And this time it won't be NULL, though it may be an empty collection.

Which could potentially be simplified to:

var lineItems = splitAccount.Select(lineItem => new RemitaSplit { /.../ });

How you simplify with LINQ in this case will depend on where/how you're populating splitAccount. I'm assuming the example in the question is just a contrived line of code to show the type of that variable, because if that's the exact code then of course that loop will never iterate over an empty list.

The point is, if splitAccount is an expression tree which will ultimately materialize data from a backing data source, you may not be able to directly call new RemitaSplit() within a .Select() until you materialize all of the records into memory, such as with a .ToList(), and there may be performance considerations to be made on that.

David
  • 208,112
  • 36
  • 198
  • 279
1

lineItems changes throughout the foreach.

Let's examine the code.

foreach ( var lineItem in splitAccount)
{
    var lineItems = new RemitaSplit { 

This translates to something like this: For each element in splitAccount create a new single reference called lineItems and assign a new RemitaSplit object to it. The type of lineItems will be RemitaSplit, it will not be List<RemitaSplit>.

Solution

I suspect you need something like the following.

using System.Linq;

(...)

var lineItems = splitAccount.Select( lineItem =>  new RemitaSplit { ... } ).ToList();

with foreach

var lineItems = new List<RemitaSplit>();
foreach ( var lineItem in splitAccount)
{
   //item, not items
   var lineItem = new RemitaSplit { 
       lineItemsId = lineItem.BursaryPaymentSplittingId.ToString(),
       (...)
   };
   lineItems.Add(lineItem);
}

Edit: json

My objective is to serialize the lineItems and post as json

using Newtonsoft.Json; 
using System.Linq;

(...)

var lineItems = splitAccount.Select( lineItem => 
    new RemitaSplit { 
        lineItemsId = lineItem.BursaryPaymentSplittingId.ToString(), 
        (...)
        }).ToList();

// Wrapping in an object, as an example, 
// the web doesn't like top level json arrays
// https://stackoverflow.com/questions/3503102/what-are-top-level-json-arrays-and-why-are-they-a-security-risk
// but what you send will be guided by the api the json is sent to. 
var json = JsonConvert.SerializeObject( new { Items = lineItems });

tymtam
  • 31,798
  • 8
  • 86
  • 126
  • This is a comment, not an answer. – DavidG Oct 22 '19 at 11:29
  • The Object `lineItems` will hold all the records gotten from the `splitAccount`. My objective is to serialize the `lineItems` and post as json to a url and i can't do that right in the `foreach`code block function. – Brainiac005 Oct 22 '19 at 11:34
  • @Brainiac005 it's a single `RemitaSplit` entry, not multiple line items. Do you want to serialize that single entry or all the RemitaSplit entries? – Panagiotis Kanavos Oct 22 '19 at 11:39
  • `var lineItems = splitAccount.Select( lineItem => new RemitaSplit { ... } ).ToList();` This worked – Brainiac005 Oct 22 '19 at 12:14