2014-01-17 66 views
2

我正在使用Linq To SQL從數據表中獲取數據。我想在where子句中使用let語句的結果。使用let in where子句的結果LinQ

如: -

from t1 in table1 
     join t2 in table2 
     on t1.field1 equals t2.field2 
     let calculatedValue = t2.val1 + t2.val2 
     join t3 in table3 
     on t2.somefield equals t3.somefield 
     into t3Grp 
     from subt3 in t3Grp.DefaultIfEmpty() 
     [select the row in table3 with where table3.someValue < calculatedValue] 

我calculatedValue面臨的proble是未在表3中.Where()條款訪問。我想完全離開加盟輸出(即,即使在具有someValue < calculatedValue表3我想在輸出該行沒有行請幫

回答

0

試試這個:

from t1 in table1 
    join t2 in table2 
    on t1.field1 equals t2.field2 
    let calculatedValue = t2.val1 + t2.val2 
    join t3 in table3.where(item => item.someValue < calculatedValue) 
    on t2.somefield equals t3.somefield 
    into t3Grp 
    from subt3 in t3Grp.DefaultIfEmpty() 
    select new 
    { 
    val1= table3.val1, 
    val2 = table3.val2, 
    . 
    . 
    . 
    } 
+0

我認爲左連接行爲將會丟失,除非where條件被指定爲「在table.Where(i => i.someValue user3206213

+0

嗨,謝謝大家的幫助。最後,我通過對DefaultIfEmpty的結果使用where子句來解決問題。就像t3Group.DefaultIfEmpty()中的t3與join t2一樣。Where(i => i == null?i.someValue == null:i.someValue user3206213

-1

你能否通過限制做到這一點。 t3Grp在您的最終select,如

from t1 in table1 
join t2 in table2 
on t1.field1 equals t2.field2 
let calculatedValue = t2.val1 + t2.val2 
join t3 in table3 
on t2.somefield equals t3.somefield 
into t3Grp 
from subt3 in t3Grp.DefaultIfEmpty() 
select new { t1, t2, t3Grp = t3Grp.Where(t3 => t3.someValue < calculatedValue) } 

值的範圍是有,我只是不知道你想要的確切投影。

+0

downvote的任何理由? – Rawling