简体   繁体   English

使用where子句LinQ的结果

[英]Using result of let in where clause LinQ

I am using Linq To SQL to get data from from data tables. 我正在使用Linq To SQL从数据表中获取数据。 I want to use the result of a let statement in a where clause. 我想在where子句中使用let语句的结果。

eg:- 例如:-

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]

The proble I face in calculatedValue is not accessible in .Where() clause of table3. .Where()子句中无法访问calculatedValue中遇到的问题。 I want fully left join output( ie; even if there is no row in table3 having someValue < calculatedValue i want that row in the output. Please help. 我想要全左联接输出(即,即使在table3中没有某行具有someValue < calculatedValue我也希望在输出中该行。请帮忙。

Try This: 尝试这个:

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,
    .
    .
    .
  }

Can you do this by restricting t3Grp in your final select , eg 您可以通过在最终select限制t3Grp来做到这一点t3Grp ,例如

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) }

The value is in scope there, I'm just not sure of the exact projection you want. 该值在范围内 ,我不确定您想要的确切投影。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM