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.