LINQ to Entities 中具有左外连接的聚合函数

发布于 2024-08-21 12:32:18 字数 2119 浏览 8 评论 0原文

我一直在这里查看相关的 LINQ 问题,试图解决这个问题,但在将我的 SQL 查询转换为等效的 LINQ to Entities 版本时遇到了一些问题。

select companies.CommpanyName,
       job.Position,
       count(offers.jobID) As Offered,
       job.Openings,
       job.Filled
from jobs
left outer join offers on  jobs.ID = offers.JobID
join membership.dbo.individuals on jobs.UserID = individuals.ID
join membership.dbo.companies on individuals.CompanyID = companies.ID
where jobs.Hidden = 0
group by offers.JobID,
         companies.CommpanyName,
         job.Position,
         job.Openings,
         job.Filled

我之前在 LINQ 中完成了左外连接 类似于这个例子,但我不确定如何将 countgroup 语句与此结合起来以获得所需的结果:

    CompanyName    Position    Offered    Openings    Filled

1   Exmaple Co.    Job X       0          2           0
2   Example Co.    Job Y       4          6           3
3   Test Co.       Job Z       1          1           1

事实使查询变得更加复杂它需要利用两个独立的数据上下文。对于缺少示例代码,我深表歉意,但我真的不知道如何开始,我的 LINQ-fu 仍然很弱。

更新:

这是我在 Craig 的帮助下找到的解决方案,由于不幸的多上下文设置,不得不使用 LINQ to Objects,JobWithOfferCounts 不是实体对象:

IEnumerable<Job> t = context1.JobSet.Include("Offers").Include("Contacts").Where(j => j.Hidden == false);
IEnumerable <JobWithOfferCounts> r = (from j in t
                 join i in context2.IndividualSet on j.UserID equals i.ID
                 join c in context2.CompanySet on i.CompanyID equals c.ID
                 select new JobWithOfferCounts()
                 {
                    JobObject = j,
                    CompanyID = Convert.ToInt32(c.ID),
                    CompanyName = c.HostName,
                    OfferCount = j.offers.Count(o => o.Rejected == false),
                    FilledCount = j.offers.Count(o => o.Accepted == true),
                    PendingCount = j.offers.Count(o => o.Accepted == false && o.Rejected == false)
                 });            

        return r;

I've been looking through related LINQ questions here trying to figure this one out, but I'm having some trouble converting a SQL query of mine to the equivalent LINQ to Entities version.

select companies.CommpanyName,
       job.Position,
       count(offers.jobID) As Offered,
       job.Openings,
       job.Filled
from jobs
left outer join offers on  jobs.ID = offers.JobID
join membership.dbo.individuals on jobs.UserID = individuals.ID
join membership.dbo.companies on individuals.CompanyID = companies.ID
where jobs.Hidden = 0
group by offers.JobID,
         companies.CommpanyName,
         job.Position,
         job.Openings,
         job.Filled

I've done left outer joins in LINQ before similar to this example but I'm not sure how to combine the count and group statements with this to get the desired result:

    CompanyName    Position    Offered    Openings    Filled

1   Exmaple Co.    Job X       0          2           0
2   Example Co.    Job Y       4          6           3
3   Test Co.       Job Z       1          1           1

The query is further complicated by the fact that it needs to utilize two separate data contexts. I apologize for the lack of example code, but I'm really not sure how to start this, my LINQ-fu is still weak.

Update:

This is the solution I arrived at with Craig's help, had to use LINQ to Objects because of the unfortunate multiple context setup, JobWithOfferCounts is not an entity object:

IEnumerable<Job> t = context1.JobSet.Include("Offers").Include("Contacts").Where(j => j.Hidden == false);
IEnumerable <JobWithOfferCounts> r = (from j in t
                 join i in context2.IndividualSet on j.UserID equals i.ID
                 join c in context2.CompanySet on i.CompanyID equals c.ID
                 select new JobWithOfferCounts()
                 {
                    JobObject = j,
                    CompanyID = Convert.ToInt32(c.ID),
                    CompanyName = c.HostName,
                    OfferCount = j.offers.Count(o => o.Rejected == false),
                    FilledCount = j.offers.Count(o => o.Accepted == true),
                    PendingCount = j.offers.Count(o => o.Accepted == false && o.Rejected == false)
                 });            

        return r;

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

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

发布评论

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

评论(1

东风软 2024-08-28 12:32:18

我不明白为什么您的查询中有individuals,或者为什么您按offers.JobID分组(与jobs.JobId不同)可能为 null,但这是第一个尝试:

var q = from c in Context.Companies     
        from i in c.Individuals
        from j in i.Jobs
        where j.Hidden == 0
        select new 
        {
            CompanyName = c.CompanyName,
            Position = j.Position,
            Offered = j.Offers.Count(),
            Openings = j.Openings,
            Filled = j.Filled
        };

使用 加入 LINQ to Entities 或LINQ to SQL

I can't see why you have individuals in your query, or why you group by offers.JobID when it (unlike jobs.JobId) could be null, but here's a first stab:

var q = from c in Context.Companies     
        from i in c.Individuals
        from j in i.Jobs
        where j.Hidden == 0
        select new 
        {
            CompanyName = c.CompanyName,
            Position = j.Position,
            Offered = j.Offers.Count(),
            Openings = j.Openings,
            Filled = j.Filled
        };

It's rarely correct to use join in LINQ to Entities or LINQ to SQL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文