本文介绍了如何找到一个行的ROW_NUMBER()使用LINQ到SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LINQ to SQL的利用了 ROW_NUMBER()分页的目的,即当您使用跳过()和Take()。

Linq to SQL makes use of ROW_NUMBER() for paging purposes, i.e. When you use Skip() and Take().

不过,我似乎无法找到实际访问 ROW_NUMBER()重视自己查询的方式。我需要找到一个查询中记录的ROW_NUMBER()(没有把所有的记录后面)。

However, I can't seem to find a way of actually accessing ROW_NUMBER() values myself in the query. I need to find the ROW_NUMBER() of a record within a query (without bringing all the records back).

我successfuly在T-SQL和LINQ to对象做到了这一点,但LINQ to SQL的解决方案被证明是难以实现的。

I've done this successfuly in T-SQL and Linq to Objects, but a Linq to SQL solution is proving elusive.

在LINQ到对象我可以得到的行数是这样的:

In Linq to Objects I can get the row number like this:

var rowTarget =
    mainQuery
        .Select((obj, index) => new { obj.ID, Index = index })
        .SingleOrDefault(x => x.ID == targetID);

// rowTarget.Index is the answer

不过,LINQ to SQL的不支持的替代选择()使用索引的参数(这是有道理的,真的 - ROW_NUMBER( )只会如果正在使用跳过()和Take()使用)。

But Linq to SQL does not support the overrides of Select() that use an index parameter (and that makes sense, really - ROW_NUMBER() would only be used if Skip() and Take() were being used).

我似乎无法找到不会导致从数据库返回的所有记录的解决方案。

I can't seem to find a solution that doesn't result in all records being returned from the database.

这甚至可能吗?

推荐答案

(编辑:试了一下,还是给了用于查询运营商不支持重载选择) ......

(edit: tried it; still gives "Unsupported overload used for query operator 'Select'")...

为什么要使用的SelectMany 在这里?你有没有试过用 选择版本 包含指数?

Why are you using SelectMany here? Have you tried it with the version of Select that includes the index?

.Select((obj, index) => new { obj.ID, Index = index })
    .SingleOrDefault(x => x.ID == targetID);

您应该还可能包括一个明确的排序依据 - LINQ到SQL让你走没有它的大部分时间,但EF不会

You should also probably include an explicit OrderBy - LINQ-to-SQL lets you get away without it most of the time, but EF doesn't.

这篇关于如何找到一个行的ROW_NUMBER()使用LINQ到SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 07:49