Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I’m working on an ASP.Net Razor-Pages  application that allows users to query a database and then export the results to an Excel spreadsheet.  

I’ve completed the code that queries the database, filters and sorts the data, then writes the result to an array.  Before writing the results to the array. I use a foreach statement to step through each record, copying each column to a variable, and then exporting the values in each column to an Excel spreadsheet.  The problem is the records are not sorted, they
 appear in that the occur in the database.  If I display the results to an array or a list the results are sorted as expected.

My code is below.  Any suggestions on how I can the records to sort (alphabetically by the PubName column).  Another option would be to copy the values from the array or list to variables and then export them to the spreadsheet.

Any suggestion on how this might be accomplished will be appreciated.  Thanks in advance


What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using PSA.Data;
using PSA.Models;

namespace PSA.Pages.MoveData
{
public class IndexModel : PageModel
{
private readonly PSA.Data.ApplicationDbContext _context;

public IndexModel(PSA.Data.ApplicationDbContext context)
{
_context = context;
}

public string PubNameSort { get; set; }
public string CurrentSort { get; set; }
public string pName { get; set; }
public string pFSGNum { get; set; }
public string pFcode { get; set; }
public int? pHours { get; set; }

//public IList<publisher> Publisher { get;set; }
public ArraySegment<publisher> Publisher { get; set; }

public async Task OnGetAsync(string sortOrder, string searchFSGNum)
{
//get user's login name
string userName = User.Identity.Name;

//get user's full name based on users login name
var fullNameQuery = from n in _context.Users.AsNoTracking()
where n.UserName == userName
select (n.LastName + ", " + n.FirstName);

string fName = await fullNameQuery.SingleAsync();

//get users fsgnum based on user' fullname
var fsgQuery = from u in _context.Publisher.AsNoTracking()
where u.PubName == fName
select u.FSGNum;

string fsgNum = await fsgQuery.SingleAsync();


//following code used to sort records by publisher name
PubNameSort = string.IsNullOrEmpty(sortOrder) ? "Name_desc" : "";


//query publishers table using FSGNum as a filter and Pubname for sort order
var publisherQuery = from p in _context.Publisher.AsNoTracking()
where p.FSGNum == fsgNum
orderby p.PubName
select p;


foreach (Publisher p in publisherQuery)
{
if (!String.IsNullOrEmpty(p.PubName))
{
pName = p.PubName;
pFSGNum = p.FSGNum;
pFcode = p.FCode;
pHours = p.Pla;

// add code to export variables to excel spreadsheet
// see document at: http://www.talkingdotnet.com/?s=import-export&submit=Search
}
}


Publisher = await publisherQuery.ToArrayAsync(); //.ToListAsync()


}
}
}
Posted
Updated 24-Aug-18 20:09pm

1 solution

On another topic, your code run the SQL query twice, firstly when you foreach, secondly when you call ToArrayAsync()

To your question, I dunno why your code is not sorted. That's the whole point of the orderby statement, it should be sorted (by PubName).
I suspect the error might lie elsewhere
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900