通过列表<t>提取项目<t>[英] Extracting Items over an List<T> of List<T>

本文是小编为大家收集整理的关于通过列表<t>提取项目<t>的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试从List<T>的List<T>中检索记录,并寻求您的帮助.

我试图在overdues.Accounts.AccountId = 'JKB1'的地方获取项目,以及如何通过以下列表项目进行操作.

public class OverdueModel
    {
        public string Slab { get; set; }
        public double Value { get; set; }
        public double Percentage { get; set; }
        public List<OverdueSlabAccounts> Accounts { get; set; }
    }

    public class OverdueSlabAccounts
    {
        public string AccountId { get; set; }
        public string AccountName { get; set; }
        public string SalesCode { get; set; }
        public string Value { get; set; }
        }


void Main(){
    List<OverdueModel> overdues = new List<OverdueModel>();
    List<OverdueSlabAccounts> accounts = new List<OverdueSlabAccounts>();

    //For T3
    accounts.Clear();
    accounts.Add(new OverdueSlabAccounts()
        {
            AccountId = "JKB1",
            AccountName = "JKB1",
            SalesCode = "JKB",
            Value = "500"
        });

    accounts.Add(new OverdueSlabAccounts()
        {
            AccountId = "JKB2",
            AccountName = "JKB2",
            SalesCode = "JKB",
            Value = "500"
        });

    overdues.Add(new OverdueModel()
    {
        Slab = "T3",
        Value = 1000,
        Percentage = 0,
        Accounts = accounts
    });

    //For T4
    accounts.Clear();
    accounts.Add(new OverdueSlabAccounts()
        {
            AccountId = "JKB1",
            AccountName = "JKB1",
            SalesCode = "JKB",
            Value = "1000"
        });

    overdues.Add(new OverdueModel()
    {
        Slab = "T4",
        Value = 1000,
        Percentage = 0,
        Accounts = accounts
    });

}

推荐答案

您可以组合使用Where和Any:

var result = overdues
                .Where(overdue => overdue.Accounts
                                     .Any(account => account.AccountId == "JKB1"));

这将过滤那些关联的任何帐户AccountId jkb1

的溢价

其他推荐答案

您可以将Linq用于目的

var filteredList = overdues.Where(x=>x.Accounts.Any(c=>c.AccountId=="JKB1"));

有关Where和Any

的更多信息
  • enumoser.where:参考
  • >参考

输出

在此处输入图像说明

其他推荐答案

您可以尝试以下方法:

 var account = accounts.Find(x => x.AccountId.Contains("JKB1")));

var account = accounts.Find(x => x.AccountId.Equals("JKB1")));

这将为您提供所需的特定帐户ID.

本文地址:https://www.itbaoku.cn/post/1557222.html

问题描述

I am trying to retrieve records from a List<T> of List<T> and seek your help in getting it.

I am trying to fetch items where overdues.Accounts.AccountId = 'JKB1' and how can i do it over the below List Items.

public class OverdueModel
    {
        public string Slab { get; set; }
        public double Value { get; set; }
        public double Percentage { get; set; }
        public List<OverdueSlabAccounts> Accounts { get; set; }
    }

    public class OverdueSlabAccounts
    {
        public string AccountId { get; set; }
        public string AccountName { get; set; }
        public string SalesCode { get; set; }
        public string Value { get; set; }
        }


void Main(){
    List<OverdueModel> overdues = new List<OverdueModel>();
    List<OverdueSlabAccounts> accounts = new List<OverdueSlabAccounts>();

    //For T3
    accounts.Clear();
    accounts.Add(new OverdueSlabAccounts()
        {
            AccountId = "JKB1",
            AccountName = "JKB1",
            SalesCode = "JKB",
            Value = "500"
        });

    accounts.Add(new OverdueSlabAccounts()
        {
            AccountId = "JKB2",
            AccountName = "JKB2",
            SalesCode = "JKB",
            Value = "500"
        });

    overdues.Add(new OverdueModel()
    {
        Slab = "T3",
        Value = 1000,
        Percentage = 0,
        Accounts = accounts
    });

    //For T4
    accounts.Clear();
    accounts.Add(new OverdueSlabAccounts()
        {
            AccountId = "JKB1",
            AccountName = "JKB1",
            SalesCode = "JKB",
            Value = "1000"
        });

    overdues.Add(new OverdueModel()
    {
        Slab = "T4",
        Value = 1000,
        Percentage = 0,
        Accounts = accounts
    });

}

推荐答案

You can use Where and Any in combination for this :

var result = overdues
                .Where(overdue => overdue.Accounts
                                     .Any(account => account.AccountId == "JKB1"));

This will filter those overdues for which associated any Account has AccountId JKB1

其他推荐答案

You could use Linq for the purpose

var filteredList = overdues.Where(x=>x.Accounts.Any(c=>c.AccountId=="JKB1"));

For more information on Where and Any

Output

enter image description here

其他推荐答案

You can try this:

 var account = accounts.Find(x => x.AccountId.Contains("JKB1")));

or

var account = accounts.Find(x => x.AccountId.Equals("JKB1")));

this will get you the specific account Id you are looking for.