Click here to Skip to main content
15,895,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,
I need to query a list which is attached to an object. Query is just to find the sum of initialValue. Since the class definition is little complex i am not able to do. please help
Below are my class structure

C#
public class Asset
    {
        public string href { get; set; }
        public int assetId { get; set; }
        public int sourceAssetId { get; set; }
        public string category { get; set; }
        public string name { get; set; }
        public Boolean isIncluded { get; set; }
        public int initialValue { get; set; }
        public int clientId { get; set; }
        public List<Portfolio> portfolioInfo { get; set; }
        public List<StockOption> stockOptionInfo { get; set; }
        public List<ConcentratedStock> concentratedStockInfo { get; set; }
    }
 public class Portfolio
    {
        public string portfolioType { get; set; }
        public List<PlanAccount> accounts { get; set; }
        public string taxStatus { get; set; }
        public Boolean isAmtApplicable { get; set; }
        public List<CurrentPortfolioAllocation> currentAllocation { get; set; }
        public List<TargetAllocation> targetAllocations { get; set; }
        public List<Fees> fees { get; set; }
        public List<PortfolioLinks> portfolioLinks { get; set; }
        public List<TrustDistributionDetails> trustDistributionDetails { get; set; }
        public List<EstateDetails> estateDetails { get; set; }
        public List<CarryForwards> carryForwards { get; set; }
    }
public class PlanAccount
    {
        public string accountSource { get; set; }
        public string accountSourceId { get; set; }
        public int officeNumber { get; set; }
        public int accountNumber { get; set; }
        public int faNumber { get; set; }
        public string clientSource { get; set; }
        public string clientSourceId { get; set; }
        public string accountType { get; set; }
        public int initialValue { get; set; }
        public List<PortfolioAllocation> allocations { get; set; }
        public Boolean attested { get; set; }
    }

My requirement is, from the PlanAccount class i need to sum up the initialValue attribute. I am expecting some LINQ approach

i need to Query Asset Object.

What I have tried:

i tried to surf in code project and net for the solution similar to my question. but not able to find a suitable answer from which i can start.

Updated
Looking for a linq query which support below code
C#
public static double PortfolioValue(Asset objasset)
        {
           double sum = 0;
           foreach(var lstpro in objasset.portfolioInfo)
           {
               foreach(var lstaccount in lstpro.accounts)
               {
                   sum+=lstaccount.initialValue;
               }
           }
           return sum;
        }
Posted
Updated 25-Feb-16 6:46am
v6
Comments
BillWoodruff 25-Feb-16 11:05am    
What happens when you run 'PortfolioValue method you show here ? Does it work ? If it works, Why do you want to rewrite this using Linq ?
jinesh sam 25-Feb-16 12:30pm    
Yes its works. Initially that piece of code was not with me..since my linq approach fails i wrote that one.

C#
List<PlanAccount> data = new List<PlanAccount>();

data.Add(new PlanAccount { initialValue = 1 });
data.Add(new PlanAccount { initialValue = 2 });
data.Add(new PlanAccount { initialValue = 3 });

// x is 6
int x = data.Sum(pa => pa.initialValue);


Updated:

C#
Asset asset = new Asset();
asset.portfolioInfo = new List<Portfolio>();
asset.portfolioInfo.Add(new Portfolio { accounts = new List<PlanAccount> { new PlanAccount { initialValue = 1 }, new PlanAccount { initialValue = 2 } } });
asset.portfolioInfo.Add(new Portfolio { accounts = new List<PlanAccount> { new PlanAccount { initialValue = 3 }, new PlanAccount { initialValue = 4 } } });

// x is 10
int x = asset.portfolioInfo.SelectMany(pi => pi.accounts).Sum(pa => pa.initialValue);
 
Share this answer
 
v2
Comments
jinesh sam 25-Feb-16 10:25am    
Thanks for prompt response. but my requirement is little different. PlanAccount is attached to Portfolio and Portfolio with Asset.

i need to query an Asset object
F-ES Sitecore 25-Feb-16 10:40am    
I've updated my solution
jinesh sam 25-Feb-16 10:50am    
getting an error " does not contain a definition for 'SelectMany' ". what might be the reason
F-ES Sitecore 25-Feb-16 11:01am    
You need this at the top of the file

using System.Linq;
jinesh sam 25-Feb-16 11:06am    
Its working... thanks: a lot :)
In addition to SiteCore's solid solution here, I'd like to add that I think it's a good idea when you have complex, nested, Linq queries, and some of the values you are "lining up" to extract and manipulate might be null/undefined ... and you don't want to deal with those values ...

Let me illustrate:

int? total = null;

if (asset != null && asset.portfolioInfo != null)
{
    total =
        asset.portfolioInfo
            // eliminate null portfolioInfos
            .Where(pi => pi != null)
            .SelectMany(acc => acc.accounts)
            // eliminate empty accounts
            .Where(act => act.initialValue != null)
            .Sum(val => (int?) val.initialValue);
}
Think about a large complex dataset you want to query against; perhaps excluding a large number of instances/records would help reduce the "cost of using Linq ?
 
Share this answer
 
Comments
jinesh sam 25-Feb-16 12:59pm    
Thanks Bill:) for your suggestions and broader approach

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