简体   繁体   中英

Count is zero after assigning to List

I have the following code in my program:

List<_Transaction> transactionListing = collectionRun.AttachedTransactions;

When I debug, the AttachedTransactions hsa a count of 3 (its also a list of List<_Transaction>). But the assignment does not work because the transactionListing has a count of zero. I'm perplexed.

EDIT: On the right hand side the count of AttachedTransactions is 3. But on the left hand side the count of transactionListing remains zero after assignment

Try this:

List<_Transaction> transactionListing = collectionRun.AttachedTransactions.ToList();

Or:

List<_Transaction> transactionListing = new List<_Transaction>(collectionRun.AttachedTransactions);

This will make a copy of the AttachedTransactions list at the time of assignment.

At the moment you're getting a reference to collectionRun.AttachedTransactions and I'm guessing that something else is altering it so it appears that the assignment isn't working.

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