如何在使用foreach循环之前对记录进行排序 [英] How to sort records before using a foreach loop

查看:302
本文介绍了如何在使用foreach循环之前对记录进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





我的尝试:



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Threading.Tasks;

使用Microsoft.AspNetCore.Mvc;

使用Microsoft.AspNetCore.Mvc.RazorPages;

使用Microsoft.EntityFrameworkCore;

使用PSA.Data;

使用PSA.Models;



名称空间PSA.Pages.MoveData

{

公共类IndexModel:PageModel

{

private readonly PSA.Data.ApplicationDbContext _context;



public IndexModel(PSA.Data.ApplicationDbContext context)

{

_context = context;

}



public string PubNameSort {get;组; } $ / $
public string CurrentSort {get;组; }

公共字符串pName {get;组; } $ / $
public string pFSGNum {get;组; }

public string pFcode {get;组; }

public int? pHour {get;组; }



//公共IList< publisher>出版商{get; set; }

public ArraySegment< publisher>出版商{get;组; } $ / $


公共异步任务OnGetAsync(字符串sortOrder,字符串searchFSGNum)

{

//获取用户登录name

string userName = User.Identity.Name;



//根据用户登录名获取用户的全名

var fullNameQuery = from _ in _context.Users.AsNoTracking()

其中n.UserName == userName

select(n.LastName +,+ n .FirstName);



string fName = await fullNameQuery.SingleAsync();



//获取用户fsgnum基于用户的全名

var fsgQuery =来自你的_context.Publisher.AsNoTracking()

其中u.PubName == fName

选择u.FSGNum;



string fsgNum = await fsgQuery.SingleAsync();





//以下用于按发布商名称排序记录的代码

PubNameSort = string.IsNullOrEmpty(sortOrder)? Name_desc:;





//使用FSGNum作为过滤器和排序顺序的Pubname来查询publishers表

var publisherQuery =来自p in _context.Publisher.AsNoTracking()

其中p.FSGNum == fsgNum

orderby p.PubName

选择p;





foreach(发布商查询中的发布商p)

{

if(!String.IsNullOrEmpty(p.PubName))

{

pName = p.PubName;

pFSGNum = p。 FSGNum;

pFcode = p.FCode;

pHours = p.Pla;



//添加代码将变量导出到excel电子表格

//请参阅以下文档:http://www.talkingdotnet.com/?s = im port-export& submit =搜索

}

}





发布者= await publisherQuery.ToArrayAsync(); //。ToListAsync()





}

}

}



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()


}
}
}

推荐答案

在另一个主题上,你的代码运行SQL查询两次,首先是你的foreach,其次是你调用ToArrayAsync()时



对于你的问题,我不知道为什么你的代码没有排序。这是orderby语句的重点,它应该被排序(通过PubName)。

我怀疑错误可能在其他地方
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


这篇关于如何在使用foreach循环之前对记录进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆