在 asp.net/C# 中使用 LinqToSql 对 row_number 进行排序

发布于 2024-11-05 00:42:17 字数 996 浏览 6 评论 0原文

我在数据库表中有一组记录,我们将其称为组件表,其定义为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掌心的温暖 2024-11-12 00:42:17

如果您绝对确定要忽略数据库编号(如果它们实际上不对应于任何内容,为什么要输出这些数字?)您可以尝试以下操作:

var gridViewData = from results in db.Components
                   where results.DisableFlag == false
                   select new
                   {
                     ComponentNames = results.Component_Name,
                     Size = results.Size__in_MB_,
                     Price = results.Price__in_SEK_,
                     TotalDownloads = results.Total_Downloads,
                     Description = results.Description
                   };

var gridViewResults = gridViewData.AsEnumerable().Select((item, index) => new
                      {
                         SNo = index + 1,
                         ComponentNames = item.ComponentNames,
                         Size = item.Size,
                         Price = item.Price,
                         TotalDownloads = item.TotalDownloads,
                         Description = item.Description
                      });

编辑:来自 如何投影行号进入 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:

var gridViewData = from results in db.Components
                   where results.DisableFlag == false
                   select new
                   {
                     ComponentNames = results.Component_Name,
                     Size = results.Size__in_MB_,
                     Price = results.Price__in_SEK_,
                     TotalDownloads = results.Total_Downloads,
                     Description = results.Description
                   };

var gridViewResults = gridViewData.AsEnumerable().Select((item, index) => new
                      {
                         SNo = index + 1,
                         ComponentNames = item.ComponentNames,
                         Size = item.Size,
                         Price = item.Price,
                         TotalDownloads = item.TotalDownloads,
                         Description = item.Description
                      });

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'"

寄居人 2024-11-12 00:42:17

大家好,这里是最终答案。约书亚做了所有的工作。非常感谢他。只是想向未来遇到同样问题的人强调答案。如果有人想投票,请投票给 Joshua

var gridViewData = from results in db.Components
                           where results.DisableFlag == false
                           select new
                           {
                               ComponentNames = results.Component_Name,
                               Size = results.Size__in_MB_,
                               Price = results.Price__in_SEK_,
                               TotalDownloads = results.Total_Downloads,
                               Description = results.Description
                           };

        var gridViewResults = gridViewData.AsEnumerable().Select((item, index) => new
        {
            SNo = index + 1,
            ComponentNames = item.ComponentNames,
            Size = item.Size,
            Price = item.Price,
            TotalDownloads = item.TotalDownloads,
            Description = item.Description
        }).ToList();

这应该可行。

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

var gridViewData = from results in db.Components
                           where results.DisableFlag == false
                           select new
                           {
                               ComponentNames = results.Component_Name,
                               Size = results.Size__in_MB_,
                               Price = results.Price__in_SEK_,
                               TotalDownloads = results.Total_Downloads,
                               Description = results.Description
                           };

        var gridViewResults = gridViewData.AsEnumerable().Select((item, index) => new
        {
            SNo = index + 1,
            ComponentNames = item.ComponentNames,
            Size = item.Size,
            Price = item.Price,
            TotalDownloads = item.TotalDownloads,
            Description = item.Description
        }).ToList();

This should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文