左外连接 LINQ To Entities

发布于 2024-09-19 14:22:29 字数 953 浏览 4 评论 0原文

我有以下实体:

客户
-- 客户端ID
-- 客户名称

承包商
-- 承包商ID
-- 承包商名称

首选承包商
-- 首选承包商 ID
-- 客户端ID
-- ContractorID

所以我有一份客户和承包商的列表。与其他承包商相比,客户更喜欢与某些承包商合作。我想构建一个 LINQ to Entity 查询,该查询使用布尔字段来提取所有承包商,指示该承包商是否是首选。

 public IQueryable<PreferredContractor> GetPreferredContractors(int clientID)
    {
        var preferredContractors = from c in db.Contractors
                  from pc in db.PreferredContractors.DefaultIfEmpty()
                  select new PreferredContractor
                             {
                                 ContractorID = c.ContractorID,
                                 ContractorName = c.ContractorName,
                                 IsPreferred = // This is where I need help
                             };

        return preferredContractors;
    }

我如何确定承包商是否是首选承包商?

I have the following entities:

Clients
-- ClientID
-- ClientName

Contractor
-- ContractorID
-- ContractorName

PreferredContractors
-- PreferredContractorID
-- ClientID
-- ContractorID

So I have a list of clients and contractors. The clients prefer to work with certain contractors than the others. I want to build a LINQ to Entity query which pulls all the contractors with a boolean field indicating whether the contractor is preferred or not.

 public IQueryable<PreferredContractor> GetPreferredContractors(int clientID)
    {
        var preferredContractors = from c in db.Contractors
                  from pc in db.PreferredContractors.DefaultIfEmpty()
                  select new PreferredContractor
                             {
                                 ContractorID = c.ContractorID,
                                 ContractorName = c.ContractorName,
                                 IsPreferred = // This is where I need help
                             };

        return preferredContractors;
    }

How can I determine if the contractor is preferred or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤檠 2024-09-26 14:23:38
    var preferredContractors =
              from c in db.Contractors
              join pc in db.PreferredContractors.Where(pc2 => pc2.ClientId == clientId) on c.ContractorId equals pc.ContractorId into j
              from pc in j.DefaultIfEmpty()
              select new PreferredContractor
                         {
                             ContractorID = c.ContractorID,
                             ContractorName = c.ContractorName,
                             IsPreferred = pc != null
                         };
    var preferredContractors =
              from c in db.Contractors
              join pc in db.PreferredContractors.Where(pc2 => pc2.ClientId == clientId) on c.ContractorId equals pc.ContractorId into j
              from pc in j.DefaultIfEmpty()
              select new PreferredContractor
                         {
                             ContractorID = c.ContractorID,
                             ContractorName = c.ContractorName,
                             IsPreferred = pc != null
                         };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文