我遇到这种情况:
我在ASP.NET中有一个表单,我需要从mssql db中提取数据。 LINQ查询是根据表单中插入的值构建的。
if (ddlRegion.SelectedIndex > 0)
{
query = query.Where(re => re.Region == ddlRegion.SelectedValue);
}
if (tbName.Text.Trim().Length > 0)
{
query = query.Where(na => na.Name.Contains(tbName.Text));
}
var result = query.Select(res => new
{
res.ColumnA,
res.ColumnB,
res.ColumnC
});
问题是我需要与TableB进行连接
query = query.Join(TableB, tA => tA.Code, tB => tB.CodFiscal, (tA, tB) => tA);
原始SQL命令是这样的:
select tA.ColumnA, tA.ColumnB, tA.ColumnC from TableA tA join TableB tB on tA.Code=tB.Code where tB.ExpireDate>=getdate() and tB.datavalabil >=getdate()
问题是来自表tB的子句加入。
答案 0 :(得分:2)
您可以这样做:
query = query.Join(TableB, tA => tA.Code, tB => tB.CodFiscal, (tA, tB) => new { tA, tB })
.Where(x => x.tB.ExpireDate >= DateTime.Now and x.tB.datavalabil >= DateTime.Now)
.Select(x => x.tA);
或者在查询语法中:
query =
from tA in query
join tB in TableB on tA.Code equals tB.CodFiscal
where tB.ExpireDate >= DateTime.Now and tB.datavalabil >= DateTime.Now
select tA;