你怎么左连接使用"与&QUOT日期;在LINQ运营商? [英] How do you left join using "date between" operator in linq?

查看:201
本文介绍了你怎么左连接使用"与&QUOT日期;在LINQ运营商?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表。父表有一个日期列,子表中有2日期列(自/至)。我需要从父左连接到子女,父母的日期列是在孩子之间。在SQL这将是这个样子:

I have two tables. The parent table has a single date column, and child table has 2 date columns (From / To). I need to make a left join from parent to child where parent's date column is between one in child. In sql this would look something like this:

select p.cob, count(*) from parent p
left join child c on p.cob between c.effective and c.expiry
group by p.cob

一个人如何写在LINQ - 我有点坚持在这里......

How does one write this in linq - I'm a bit stuck here....

推荐答案

这应该是你在找什么

var query = from p in context.Parent
            from c in context.Child.Where(x => p.cob >= x.effective)
                                   .Where(x => p.cob <= x.expiry)
                                   .DefaultIfEmpty()
            group p by p.cob into pg
            select new
            {
              cob = pg.Key,
              count = pg.Count()
            };

这篇关于你怎么左连接使用&QUOT;与&QUOT日期;在LINQ运营商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆