LINQ IN类型查询,以根据另一个查询的结果集获取记录

时间:2018-04-04 10:45:23

标签: c# linq odata

UserAuthorizations表具有公司每用户明智,我想获得分配给他们的用户的公司列表。

        // GET: odata/Companies/GetUserCompanies
        [EnableQuery]
        public IQueryable<UserAuthorization> GetUserCompanies()
        {
            List<int> userCompanyIds = db.UserAuthorizations.Where(u => u.Cwid == this.UserId).Select(s => s.CompanyId).ToList();
            return db.Companies.Where(m => m.Id.ToString().Contains(userCompanyIds));

        }

对于上面我得到的错误就像

Cannot convert from 'System.Collections.Generic.List<int>' to 'string'

enter image description here

1 个答案:

答案 0 :(得分:5)

试试吧

        public IQueryable<Company> GetUserCompanies()
        {
            List<int> userCompanyIds = db.UserAuthorizations.Where(u => u.Cwid == this.UserId).Select(s => s.CompanyId).ToList();
            return db.Companies.Where(m => userCompanyIds.Contains(m.Id));
        }