2014-11-25 82 views
-1

我有这样的LINQ查询我运行一个搜索功能(DateFrom和DateTo是datetime对象):LINQ查询联接表

var result = from x in DbContext.Transaction_Groups 
where (x.Date_Created >= DateFrom && x.Date_Created <= DateTo) 
select x; 

这些都是我的表:

enter image description here

我试图将这两张表加在一起,以便我可以使用.Transaction_Date_Time而不是我查询中的.Date_Created function

我的查询试图确定DateFrom(用户选择的值)和DateTo(另一个用户选择的值)之间的所有行。

+1

[加入例子的LINQ(http://www.dotnetperls.com/join) – MethodMan 2014-11-25 13:36:06

回答

1

你想通过Group_ID加入吗?

var result = from x in DbContext.Transaction_Groups 
join tran in DbContext.CashDeposit on x.Group_ID equals tran.Group_ID 
where (tran.Transaction_Date_Time >= DateFrom && tran.Transaction_Date_Time <= DateTo) 
select tran; 
+0

我真的觉得很奇怪给一个表的名称是其他表的起始的变量。事实上,它很混乱,就像使用单字母变量一样。 – Nzall 2014-11-25 13:39:34

1
var result = from transgroup in DbContext.Transaction_Groups 
      join cashDep in DbContext.CashDeposit 
      on transgroup.Group_ID equals cashDep.Group_ID 
      where (cashDep.Transaction_Date_Time >= DateFrom 
        && cashDep.Transaction_Date_Time <= DateTo) 
      select transgroup 

未经测试,但应该工作。为了清晰起见,缩进。