问题描述
我正在尝试从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.
问题描述
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
其他推荐答案
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.