如果我有一个Company
对象:
var companies = db.Companies.Where(...);
以及User
与Company
object相关的对象one-to-many
:
var users = db.Users.Where(...);
我如何才能达到这样的标准 select * from users where user.company in (company)
从一些文章中阅读,我尝试了类似的东西:
users.Where(x => companies.Contains(x.company))
但似乎不起作用。我在这里错过了什么?
编辑
准确答案:
public PartialViewResult IndexGrid(String search)
{
var companies = db.Users.Find(User.Identity.GetUserId()).Companies.AsQueryable();
Guid[] guids = companies.Select(c => c.Id).ToArray();
if (String.IsNullOrEmpty(search))
return PartialView("_IndexGrid", db.Set<Quotation>().OrderByDescending(x => x.Code).AsQueryable()
.Where(x => guids.Contains(x.Company.Id)));
else
return PartialView("_IndexGrid", db.Set<Quotation>().OrderByDescending(x => x.Code).AsQueryable()
.Where(x => guids.Contains(x.Company.Id))
.Where(x => x.Code.Contains(search)|| x.MasterCustomer.Name.Contains(search)));
}
当您使用最后一个变体作为
users.Where(x => companies.Contains(x.company))
.Net 不知道如何比较 Companies 并使用对相等对象的引用。如果您的公司有 id 属性,请尝试此操作。如果没有,请告诉我。
users.Where(x => companies.Select(c => c.Id).Contains(x.company.Id))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句