如何查询附加到对象的列表c# [英] How to query list which is attached to an object c#

查看:100
本文介绍了如何查询附加到对象的列表c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

我需要查询附加到对象的列表。查询只是为了找到 initialValue 的总和。由于类定义很复杂,我无法做到。请帮助

以下是我的班级结构



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

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; }
    }



我的要求是,从 PlanAccount 类我需要总结 initialValue 属性。我期待一些LINQ方法



i需要查询资产对象。



我有什么尝试过:



i尝试在代码项目和网络中进行冲浪,寻找类似于我的问题的解决方案。但是我无法找到合适的答案。



更新

正在寻找一个linq查询下面的代码支持


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

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;
        }

推荐答案

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:

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);


除了SiteCore在这里的可靠解决方案之外,我想补充一点我认为当你有复杂的,嵌套的Linq查询时,这是一个好主意,而你正在排队提取和操作的一些值可能是null / undefined ......并且你不想处理这些值。 ..



让我说明一下:



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);
}

想想要查询的大型复杂数据集;也许排除大量实例/记录会有助于降低使用Linq的成本?

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 ?


这篇关于如何查询附加到对象的列表c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆