本文介绍了您如何使用“之间的日期"离开联盟? 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()
            };

这篇关于您如何使用“之间的日期"离开联盟? LINQ中的运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 02:39