Linq to Entities - 子查询在where语句中 [英] Linq to Entities - Subquery in the where statement

查看:157
本文介绍了Linq to Entities - 子查询在where语句中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一定很简单,但是我一直在寻找2个小时,找不到答案。如何在Linq中写入这个实体:

This must be simple, but I've been searching for 2 hours, and can't find an answer. How do I write this in Linq to Entities:

SELECT Reg4, Reg5, Reg6
FROM dbo.Table1
WHERE Reg1 = 15
AND Reg2 = ( SELECT MAX(Reg2) FROM dbo.Table2 WHERE Reg1 = 15);

是否可以在查询表达式和基于方法的语法中执行此操作?

Tks

Is it possible to do it both in query expressions and method based syntaxes?
Tks

推荐答案

var r1 = 15;
var q = from t in Context.Table1
        where t.Reg1 == r1 && 
              t.Reg2 == Context.Table2
                               .Where(t2 => t2.Reg1 == r1)
                               .Max(t2 => t2.Reg2)
        select t;

如果您有从Table1到Table2的导航/关联,则更容易。但是你没有表现出来,所以我也不会....

Easier still if you have a navigation/association from Table1 to Table2. But you didn't show that, so neither will I....

这篇关于Linq to Entities - 子查询在where语句中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆