在 asp.net/C# 中使用 LinqToSql 对 row_number 进行排序
我在数据库表中有一组记录,我们将其称为组件表,其定义为 follows。
管理员可以使用表的最后一列禁用标志来禁用某些组件。如果某个特定组件被禁用,它不应出现在用户的网格视图中。
我正在从数据库获取数据并通过 gridview 呈现,如此处所示,如果您观察到SNo 值不按顺序排列。
我用来检索数据的 linq 查询是:
var gridViewResults = from results in db.Components where results.DisableFlag == false
select new { SNo = results.SNo, ComponentNames = results.Component_Name, Size = results.Size__in_MB_, Price = results.Price__in_SEK_, TotalDownloads = results.Total_Downloads, Description = results.Description };
但我希望数据按顺序显示,即 SNo 为 1、2、3、4,而不依赖于数据库表 SNO 值:以供参考这个。
我无法弄清楚如何使用 linq 查询来实现此目的:
我尝试过这个查询:
(db.Components.AsEnumerable().Select((iterator)=> new{iterator.SNo + 1})
但我认为这是荒谬的。有人可以帮我解决这个问题吗?
感谢期待。
I have set of records in a database table lets call it Components Table which is defined as follows.
The administrator can disable some of the components using disableflag which is the last column of the table. If a particular component is disabled it should not appear in the gridview of the user.
I'm getting the data from the database and presenting through the gridview as shown here, if you observe the SNo values are not in order.
The linq query that i'm using to retrieve the data is:
var gridViewResults = from results in db.Components where results.DisableFlag == false
select new { SNo = results.SNo, ComponentNames = results.Component_Name, Size = results.Size__in_MB_, Price = results.Price__in_SEK_, TotalDownloads = results.Total_Downloads, Description = results.Description };
But I want the data to be shown in order meaning with SNo to be 1, 2, 3, 4 with out dependency on the database table SNO values: for reference look at this.
I'm not able to figure out how to use the linq query to achieve this:
I have tried this query:
(db.Components.AsEnumerable().Select((iterator)=> new{iterator.SNo + 1})
But i think it is absurd. Can some one help me out on this.
Thanks in anticipation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您绝对确定要忽略数据库编号(如果它们实际上不对应于任何内容,为什么要输出这些数字?)您可以尝试以下操作:
编辑:来自 如何投影行号进入 Linq 查询结果
EDIT2:修复 SQL 不支持的选择: Linq 错误 - “NotSupportedException:查询运算符使用的重载不受支持'选择'"
If you're absoutely certain you want to ignore the database numbers (why output the numbers if they don't actually correspond to anything?) you may be able to try the following:
EDIT: Alternate solution from How To Project a Line Number Into Linq Query Results
EDIT2: Fix for unsupported select by SQL: Linq error - "NotSupportedException: Unsupported overload used for query operator 'Select'"
大家好,这里是最终答案。约书亚做了所有的工作。非常感谢他。只是想向未来遇到同样问题的人强调答案。如果有人想投票,请投票给 Joshua
这应该可行。
Hi everyone here is the final answer. Joshua did all of the work. A big thanks to him. Just want to highlight the answer to anyone with the same problem for the future. If any one want to vote up please vote for Joshua
This should work.