【问题标题】:How to use Linq to query a table dependent on where a user belongs to another table如何使用 Linq 根据用户属于另一个表的位置来查询表
【发布时间】:2021-05-27 15:55:11
【问题描述】:

我有一个简单的问题困扰着我,我想了解如何使用 linq 查询数据子集,例如我有一个包含证书的表,证书属于一家公司,一家公司有属于它的用户,并且该视图基本上应该只显示当前用户链接到的公司的证书。这就是我目前拥有的,并且正在与存在语句或子查询的正确语法作斗争?

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;
}

【问题讨论】:

  • 所以发布你的模型类和答案会神奇地出现。

标签: c# sql entity-framework linq linq-to-sql


【解决方案1】:

这应该会返回一个不同的证书列表,这些证书属于分配给 _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()
```

【讨论】:

    【解决方案2】:

    我设法用一种更简单的方法解决了我的问题,而且看起来并不难。上面的解决方案让我很困惑,我没有实现它。请在下面找到更简单的解决方案。我只需要添加带有布尔条件的 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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多