加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

如何在LINQ-to SQL中编写这个交叉应用查询?

发布时间:2020-12-12 16:31:26 所属栏目:MsSql教程 来源:网络整理
导读:我有以下表格: create table TableA ( Id int primary key identity,Key int not null)create table TableB ( Id int primary key identity,TableA_Id int not null foreign key references TableA(Id),Value varchar(80) not null) 我想使用lambda表示法在L
我有以下表格:
create table TableA (
    Id int primary key identity,Key int not null
)

create table TableB (
    Id int primary key identity,TableA_Id int not null foreign key references TableA(Id),Value varchar(80) not null
)

我想使用lambda表示法在LINQ-to-SQL中编写以下查询:

select TableA.Key,b.Value
from TableA
cross apply (
    select top 10 TableB.Value
    from TableB
    where TableA.Id = TableB.TableA_Id
    order by TableB.Value
) b
where TableA.Key between 0 and 999

我该怎么做?

解决方法

这应该是诀窍
var query = from a in context.TableA
            from b in context.TableB
                             .Where(x => x.TableA_Id == a.Id)
                             .OrderBy(x => x.Value)
                             .Take(10)
            where a.Key >= 0 && a.Key <= 999
            select new
            {
              a.Key,b.Value,};

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读