如何使用 Linq 根据用户属于另一个 table 的位置查询 table
How to use Linq to query a table dependent on where a user belongs to another table
我有一个 运行 我身边的简单问题,我想了解如何使用 linq 来查询数据子集,例如我有一个包含证书的 table,证书属于一家公司,一家公司有属于它的用户,并且该视图基本上应该只显示当前用户链接到的公司的证书。这就是我目前所拥有的,并且正在与存在语句或子查询的正确语法作斗争?
public List<CertificateListItemModel> GetUserCertificates()
{
var certificates = (from p in _db.Certificates
**where(
from bu2 in _db.BusinessUsers
where p.BusinessId == bu2.BusinessId && bu2.Email == _user.Name
//select new {}
)**
select new CertificateListItemModel
{
...
})
.Distinct().ToList();
return certificates;
}
这应该 return 您属于公司 _user 分配给的不同证书列表(假设 _user.Name 和 businessUser.Email 如果相同,则确实具有相同的内容用户)。
var certificates = _db.Certificates
.Join(_db.Businesses,
certificate => certificate.BusinessId,
business => business.Id,
(certificate, business) => new { certificate, business })
.Join(_db.BusinessUsers.Where(user => user.Email = _user.Name),
certificateAndBusiness => certificateAndBusiness.business.Id,
businessUser => businessUser.BusinessId,
(certificateAndBusiness, businessUser) => certificate)
.GroupBy(certificate => certificate.Id)
.Select(certificates => certificates.First())
.Select(certificate => new CertificateListItemModel() {...})
.ToList()
```
我设法用更简单的方法解决了我的问题,看起来并没有那么困难。上面的解决方案让我很困惑,我没有实现它。请在下面找到更简单的解决方案。我只需要添加带有布尔条件的 Where
var applications = (_db.Certificates
.Join(_db.BillingItems, p => p.CertificateId, bi => bi.CertificateId, (p, bi) => new {p, bi})
.Where(@t => (_db.BusinessUsers.Any(c => c.CbrBusinessId == @t.p.CbrOwnerRef && c.Email == _user.Name)))
.Select(@t => new CertificateListItemModel
{
CertificateId = @t.p.CertificateId,
ApplicationRefNo = @t.p.ApplicationReferenceNo,
ApplicationStatus = @t.p.ApplicationStatus,
SubmittedDateTime = @t.p.SubmittedDateTime,
IssuingLocation = @t.p.DesiredIssueLocation,
BillingId = @t.bi.BillingId,
PaperNo = @t.bi.PaperNo,
InvoiceNo = @t.bi.InvoiceNo,
BillingStatus = @t.bi.BillingStatus,
InvoiceRefNo = @t.bi.PaperNo,
BillingParty = @t.bi.BillingParty
}))
.Distinct().ToList();
return applications;
我有一个 运行 我身边的简单问题,我想了解如何使用 linq 来查询数据子集,例如我有一个包含证书的 table,证书属于一家公司,一家公司有属于它的用户,并且该视图基本上应该只显示当前用户链接到的公司的证书。这就是我目前所拥有的,并且正在与存在语句或子查询的正确语法作斗争?
public List<CertificateListItemModel> GetUserCertificates()
{
var certificates = (from p in _db.Certificates
**where(
from bu2 in _db.BusinessUsers
where p.BusinessId == bu2.BusinessId && bu2.Email == _user.Name
//select new {}
)**
select new CertificateListItemModel
{
...
})
.Distinct().ToList();
return certificates;
}
这应该 return 您属于公司 _user 分配给的不同证书列表(假设 _user.Name 和 businessUser.Email 如果相同,则确实具有相同的内容用户)。
var certificates = _db.Certificates
.Join(_db.Businesses,
certificate => certificate.BusinessId,
business => business.Id,
(certificate, business) => new { certificate, business })
.Join(_db.BusinessUsers.Where(user => user.Email = _user.Name),
certificateAndBusiness => certificateAndBusiness.business.Id,
businessUser => businessUser.BusinessId,
(certificateAndBusiness, businessUser) => certificate)
.GroupBy(certificate => certificate.Id)
.Select(certificates => certificates.First())
.Select(certificate => new CertificateListItemModel() {...})
.ToList()
```
我设法用更简单的方法解决了我的问题,看起来并没有那么困难。上面的解决方案让我很困惑,我没有实现它。请在下面找到更简单的解决方案。我只需要添加带有布尔条件的 Where
var applications = (_db.Certificates
.Join(_db.BillingItems, p => p.CertificateId, bi => bi.CertificateId, (p, bi) => new {p, bi})
.Where(@t => (_db.BusinessUsers.Any(c => c.CbrBusinessId == @t.p.CbrOwnerRef && c.Email == _user.Name)))
.Select(@t => new CertificateListItemModel
{
CertificateId = @t.p.CertificateId,
ApplicationRefNo = @t.p.ApplicationReferenceNo,
ApplicationStatus = @t.p.ApplicationStatus,
SubmittedDateTime = @t.p.SubmittedDateTime,
IssuingLocation = @t.p.DesiredIssueLocation,
BillingId = @t.bi.BillingId,
PaperNo = @t.bi.PaperNo,
InvoiceNo = @t.bi.InvoiceNo,
BillingStatus = @t.bi.BillingStatus,
InvoiceRefNo = @t.bi.PaperNo,
BillingParty = @t.bi.BillingParty
}))
.Distinct().ToList();
return applications;