Linq to Join tables 基于两者之间的日期

Linq to Join tables based on date in between

CREATE Table A
(StartDate DATETIME, 
EndDate DATETIME,
Price float)

CREATE Table B
(EfectiveDate DATETIME,
Item VARCHAR(50))

INSERT INTO A values ('01/20/2016', '11/12/2017', '2.40')
INSERT INTO A values ('11/13/2017', '03/02/2019', '1.20')
INSERT INTO A values ('03/03/2019', '05/22/2019', '2.20')
INSERT INTO A values ('05/23/2019', '07/23/2019', '1.90')

INSERT INTO B values ('06/21/2019' 'Pen')
INSERT INTO B values ('01/01/2018','Pencil')

我需要根据 table 中的生效日期组合这 2 tables unsing LINQ B 应该在 table 中的 StartDate 和 EndDate 之间 A

类似...

Select * FROM B
INNER JOIN A on B.EfectiveDate BETWEEN A.StartDate AND A.EndDate

from tblB in B 
join tblA in A on tblB.B.EfectiveDate BETWEEN tblA.StartDate AND tblA.EndDate

显然我上面的 LINQ 查询是错误的,但我希望你能理解。

您不能特别加入 BETWEEN 条件

您可以尝试使用笛卡尔积,然后根据适当的条件进行过滤:

from tblB in B
from tblA in A
where tblB.EfectiveDate >= tblA.StartDate && tblB.EfectiveDate <=  tblA.EndDate
select new {
    StartDate = tblA.StartDate,
    EndDate = tblA.EndDate,
    Price = tblA.Price,
    EfectiveDate = tblB.EfectiveDate,
    Item = tblB.Item
};