Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two objects named Transactions and CreditorDetails . I want to be able to get the retrieve say CFirstName from my transations object. I don't want to retrieve it from CreditorDetails. for example, i want to be able to say

C#
foreach ( Transactions item in info.transaction)
                    {
      Console.WriteLine("My Name" + item.creditors.Select(m =>m.CFirstName));
                    }




The above code isn't giving me the desired result. it's returning system.linq.Enumerable+whereSelectListIterator........... which i think i know why but i need to be able to get the CFirstName from Transaction object.

What I have tried:

C#
<pre> class Transactions
    {
        public List<CreditorsDetails> creditors { get; set; }
}


C#
<pre> class CreditorsDetails {
        public string CFirstName { get; set; }
        public string CAddress { get; set; }
        public string CCountry { get; set; }
    }
Posted
Updated 10-Dec-17 22:29pm

Try:
item.creditors.Select(m =>m.CFirstName).FirstOrDefault()
 
Share this answer
 
Comments
Mcbaloo 11-Dec-17 4:28am    
item.creditors contains two items . What i have noticed is that after applying FirstorDefault, only the first item is printed out(twice). I want the two items out.
F-ES Sitecore 11-Dec-17 4:34am    
Then you'll need a loop

foreach ( Transactions item in info.transaction)
{
foreach (var fn in item.creditors.Select(m =>m.CFirstName))
{
....
}
}
OriginalGriff 11-Dec-17 4:49am    
If you need more than one item returned, then you have to have a collection as the response - which is exactly what your original code was doing. It prints "system.linq.Enumerable+whereSelectListIterator" because that is the name of the class because it can't resolve the content to a single item, which WriteLine expects.
To print more than one item, you need to iterate over the collection that the Linq request returns inside your existing loop.
Apparently a Transactions instance can have multiple CreditorsDetails.
So item.creditors.Select(m => m.CFirstName) will yield an enumeration of creditors names.
You can try item.creditors.Select(m => m.CFirstName).FirstOrDefault(), but I suspect there is a flaw in your design.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900