如何在 LINQ-to-SQL 中编写此交叉应用查询? [英] How do I write this cross apply query in LINQ-to-SQL?

查看:13
本文介绍了如何在 LINQ-to-SQL 中编写此交叉应用查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下表格:

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 中编写以下查询:

I would like to write the following query in LINQ-to-SQL using lambda notation:

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

这篇关于如何在 LINQ-to-SQL 中编写此交叉应用查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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