我有以下型号和清单

public class MCashTransactions
{
    public int id { get; set; }
    public int CashAccountId { get; set; }

}

public class MCashAccount
{
    public int id { get; set; }

}

 List<MCashTransactions> CashTransactions = new List<.MCashTransactions>();
 List<MCashAccount> CashAccounts = new List<MCashAccount>();


我想用属性List<.MCashTransactions> CashTransactions和列表CashAccountIdid过滤列表List<MCashAccount> CashAccounts

到目前为止,我所做的只是迭代并检查每次迭代是否存在。有什么更好的方法可以使用linq实现呢?

for (int i = 0; i < CashTransactions.Count; i++)
        {
         for (int j = 0; j < CashAccounts.Count; j++)
          {
            if (CashTransactions[i].CashAccountId==CashAccounts[i].id)
              {
              //work to be done here
              }
           }
        }

最佳答案

是的,您可以在两个列表上执行join

var joined = from ct in CashTransactions
             join ca in CashAccounts
                 on ct.CashAccountId equals ca.id
             select new { CashTransaction = ct, CashAccount = ca };

foreach (var item in joined)
{
    //Do whatever with item.CashAccount and item.CashTransaction
}

关于c# - 遍历两个列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24843872/

10-11 22:28
查看更多