如何计算外键项?

时间:2012-04-18 07:06:09

标签: c# linq entity-framework entity-framework-4.1

我需要获取所有bank BankBranches cityId = filter.CityId > 0

代码:

 banks = from bank in banks
            where bank.BankBranches.Where(y => y.CityId == filter.CityId).Count() > 0
            select bank;
    var list = banks.ToList();

错误:

  

不允许从'Model.Entities.BankBranch'访问成员'Int32 CityId'   'System.Linq.IQueryable`a [Model.Entities.BankBranch]。

2 个答案:

答案 0 :(得分:2)

 banks = from bank in banks.Include(p=>p.BankBranches)
            where bank.BankBranches.Where(y => y.CityId == filter.CityId).Count() > 0
            select bank;
    var list = banks.ToList();

答案 1 :(得分:0)

banks = from bank in banks
        join bankBranche in m_banksRepository.BankBranches on bank.Id equals bankBranche.BankId
        where bankBranche.CityId == filter.CityId
        select bank;