Ordering the row_number using LinqToSql in asp.net/C#

寵の児 提交于 2019-12-24 05:12:21

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/5873582/ordering-the-row-number-using-linqtosql-in-asp-net-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!