C# 非等键上的LINQ连接
我需要将此SQL查询转换为LINQC# 非等键上的LINQ连接,c#,sql,.net,linq,C#,Sql,.net,Linq,我需要将此SQL查询转换为LINQ SELECT * FROM Table1 t1 JOIN Table2 t2 ON t1.LogDate >= t2.StartDate AND t1.LogDate <= t2.EndDate 选择* 来自表1 t1 在t1.LogDate>=t2.StartDate和t1.LogDate上连接表2 t2 LINQ只支持在相等键上连接,我建议使用交叉连接。根据查询优化器的不同,它可能会转换为内部联接 from t1 in Table1 fr
SELECT *
FROM Table1 t1
JOIN Table2 t2 ON t1.LogDate >= t2.StartDate AND t1.LogDate <= t2.EndDate
选择*
来自表1 t1
在t1.LogDate>=t2.StartDate和t1.LogDate上连接表2 t2 LINQ只支持在相等键上连接,我建议使用交叉连接。根据查询优化器的不同,它可能会转换为内部联接
from t1 in Table1
from t2 in Table2
where t1.LogDate >= t2.StartDate && t1.LogDate <= t2.EndDate
select new { t1, t2 }
表1中t1的
来自表2中的t2
如果t1.LogDate>=t2.StartDate&&t1.LogDateLINQ只支持在相等键上进行连接,我建议使用交叉连接。根据查询优化器的不同,它可能会转换为内部联接
from t1 in Table1
from t2 in Table2
where t1.LogDate >= t2.StartDate && t1.LogDate <= t2.EndDate
select new { t1, t2 }
表1中t1的
来自表2中的t2
其中t1.LogDate>=t2.StartDate&&t1.LogDate如果您要加入这些表,希望您能够提前考虑,并在表2到表1中使用外键引用设计数据库。假设您这样做了,Lambda表达式将如下所示:
表1表2
PK->ID FK->T1\U ID
db.Table1.Join(db.Table2.ToList(),
t1 => t1.ID,
t2 => t2.T1_ID,
(t1, t2) => new { Table1, Table2})
.Where(result => result.Table1.LogDate >= result.Table2.StartDate &&
result.Table1.LogDate <= result.Table2.EndDate);
db.Table1.Join(db.Table2.ToList(),
t1=>t1.ID,
t2=>t2.T1_ID,
(t1,t2)=>新的{表1,表2})
。其中(result=>result.Table1.LogDate>=result.Table2.StartDate&&
result.Table1.LogDate如果要连接这些表,希望您事先考虑,并在表2到表1中使用外键引用设计数据库。假设您这样做,Lambda表达式如下所示:
表1表2
PK->ID FK->T1\U ID
db.Table1.Join(db.Table2.ToList(),
t1 => t1.ID,
t2 => t2.T1_ID,
(t1, t2) => new { Table1, Table2})
.Where(result => result.Table1.LogDate >= result.Table2.StartDate &&
result.Table1.LogDate <= result.Table2.EndDate);
db.Table1.Join(db.Table2.ToList(),
t1=>t1.ID,
t2=>t2.T1_ID,
(t1,t2)=>新的{表1,表2})
。其中(result=>result.Table1.LogDate>=result.Table2.StartDate&&
result.Table1.LogDate我在这里没有看到问题!!没有尝试解决这个问题吗?你尝试过寻找基本的Linq示例吗?例如:我在这里没有看到问题!!没有尝试解决这个问题吗?你尝试过寻找基本的Linq示例吗?例如:@VDohnal-它很可能会被转换成内部连接
@VDohnal-它最像可以转换为内部联接