1
var list = new List<ListCreaditInBankView>();
var banktemp = m_banksRepository.Banks;
foreach (Bank bank in banktemp)
{
    var bankbranchtemp = m_banksRepository.BankBranches.Where(x => x.BankId == bank.Id);
    foreach (BankBranch bankBranch in bankbranchtemp)
    {
        var creditortemp = m_creditorsRepository.Creditors.Where(x => x.BankBranchId == bankBranch.Id);
        list.Add(new ListCreaditInBankView(){Bank = bank, Creditors = creditortemp});

    }

}

我需要得到List<ListCreaditInBankView>没有这些周期。

我试过了,但它只得到一个Creditors

var lists = (from bank in banksTemp
                         let creditorBank = m_creditorsRepository.GetCreditorBank(bank.BankBranches.Select(x => x.Id).ToList())
                         select new ListCreaditInBankView() {Bank = bank, Creditors = creditorBank}).ToList();
4

2 回答 2

1

尝试以下操作:

var lists = (from bank in m_banksRepository.Banks
             select new ListCreaditInBankView
             {
                 Bank = bank,
                 Creditors = creditorsRepository.GetCreditorBank(bank.BankBranches
                     .Select(x => x.Id).ToList())
             }).ToList();

或者,如果您更喜欢其他风格的 LINQ(method chaining它被称为,感谢 Numan :)):

var lists = m_banksRepository.Banks
             .Select(bank => new ListCreaditInBankView
             {
                 Bank = bank,
                 Creditors = creditorsRepository.GetCreditorBank(bank.BankBranches
                     .Select(x => x.Id).ToList())
             }).ToList();
于 2012-04-20T10:55:20.137 回答
0

如果您使用的是 Linq 2 Entity 框架,您可以尝试另一种方法。

http://msdn.microsoft.com/en-us/library/bb896272.aspx

您只需要确保您已正确设置数据库中的表和键(或至少在生成的模型中进行映射),否则它将无法正常工作。

于 2012-04-20T14:00:08.313 回答