在where子句LinQ中使用let的结果

时间:2014-01-17 11:00:21

标签: c# linq

我正在使用Linq To SQL从数据表中获取数据。我想在where子句中使用let语句的结果。

例如: -

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]

我在computeValue中遇到的问题无法在table3的.Where()子句中访问。我想完全左连接输出(即;即使table3中没有行someValue < calculatedValue我想要输出中的那行。请帮忙。

2 个答案:

答案 0 :(得分: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,
    .
    .
    .
  }

答案 1 :(得分:-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) }

的范围,我只是不确定你想要的确切投影。