C# 每次调用此方法时,我都要维护此方法中的totalNumberOfRecords

C# 每次调用此方法时,我都要维护此方法中的totalNumberOfRecords,c#,C#,我经常调用此方法,但当searchcolumn是第一个时,它必须调用totalNumberOfRecords方法。下次调用此方法时,它应该保持totalNumberOfRecords public static EGResponse<List<Assets>> GetPlantAttributes(string templateId, string searchColumn, string searchValue, string pageCount) {

我经常调用此方法,但当searchcolumn是第一个时,它必须调用totalNumberOfRecords方法。下次调用此方法时,它应该保持totalNumberOfRecords

public static EGResponse<List<Assets>> GetPlantAttributes(string templateId, string searchColumn, string searchValue, string pageCount)
    {
        string query = string.Empty;
        //int totalNumberOfRecords = 0;
        List<Assets> summary = null;
        DateTime currentdate=DateTime.Now;
        EGResponse<List<Assets>> resp = new EGResponse<List<Assets>>(EGResult.Failure, null, EGDataLayer.CommonUtility.OPERATIONFAIL);
        int totalNumberOfRecords;

        if (!string.IsNullOrEmpty(searchColumn) && searchColumn == "Empty")
        {
            totalNumberOfRecords = 0;
            pageCount = "0";
        }
        else
        {
            ////if (!string.IsNullOrEmpty(searchValue) && searchValue == "Empty" && totalNumberOfRecords<0)
            //{
            //    totalNumberOfRecords = GetCountOfPlantAttributes(searchColumn, searchValue);
            //}

            //totalNumberOfRecords = GetCountOfPlantAttributes(searchColumn, searchValue);
            if (pageCount.ToLower() == "first" && !string.IsNullOrEmpty(searchValue) )
            {
                pageCount = "0";
                totalNumberOfRecords = GetCountOfPlantAttributes(searchColumn, searchValue);
            }

            if (pageCount.ToLower() == "last")
            {
                //totalNumberOfRecords = GetCountOfPlantAttributes(searchColumn, searchValue);
                pageCount = (totalNumberOfRecords / 20).ToString();
            }
        }
publicstaticegresponse GetPlantAttributes(stringtemplateid、stringsearchcolumn、stringsearchvalue、stringpagecount)
{
string query=string.Empty;
//int totalNumberOfRecords=0;
列表摘要=null;
DateTime currentdate=DateTime.Now;
EGResponse resp=新的EGResponse(EGResult.Failure,null,EGDataLayer.CommonUtility.OPERATIONFAIL);
int totalNumberOfRecords;
如果(!string.IsNullOrEmpty(searchColumn)&&searchColumn==“Empty”)
{
totalNumberOfRecords=0;
pageCount=“0”;
}
其他的
{
////如果(!string.IsNullOrEmpty(searchValue)&&searchValue==“Empty”&&totalNumberOfRecords
下次我调用这个方法时,它应该保持
记录总数

public static EGResponse<List<Assets>> GetPlantAttributes(string templateId, string searchColumn, string searchValue, string pageCount)
    {
        string query = string.Empty;
        //int totalNumberOfRecords = 0;
        List<Assets> summary = null;
        DateTime currentdate=DateTime.Now;
        EGResponse<List<Assets>> resp = new EGResponse<List<Assets>>(EGResult.Failure, null, EGDataLayer.CommonUtility.OPERATIONFAIL);
        int totalNumberOfRecords;

        if (!string.IsNullOrEmpty(searchColumn) && searchColumn == "Empty")
        {
            totalNumberOfRecords = 0;
            pageCount = "0";
        }
        else
        {
            ////if (!string.IsNullOrEmpty(searchValue) && searchValue == "Empty" && totalNumberOfRecords<0)
            //{
            //    totalNumberOfRecords = GetCountOfPlantAttributes(searchColumn, searchValue);
            //}

            //totalNumberOfRecords = GetCountOfPlantAttributes(searchColumn, searchValue);
            if (pageCount.ToLower() == "first" && !string.IsNullOrEmpty(searchValue) )
            {
                pageCount = "0";
                totalNumberOfRecords = GetCountOfPlantAttributes(searchColumn, searchValue);
            }

            if (pageCount.ToLower() == "last")
            {
                //totalNumberOfRecords = GetCountOfPlantAttributes(searchColumn, searchValue);
                pageCount = (totalNumberOfRecords / 20).ToString();
            }
        }
不,这是不可能的,因为它是局部变量(方法的局部变量)如下面的代码所示,一旦方法执行结束,它将超出范围。如果您想保留它,请使用某种存储机制。在平面文件、数据库或缓存等中。如果它是任何web应用程序,那么您可以使用
会话
,但从您的帖子中不确定这一点

public static EGResponse<List<Assets>> GetPlantAttributes(string templateId, string searchColumn, string searchValue, string pageCount)
    {
        string query = string.Empty;
        .....
        int totalNumberOfRecords;
publicstaticegresponse GetPlantAttributes(stringtemplateid、stringsearchcolumn、stringsearchvalue、stringpagecount)
{
string query=string.Empty;
.....
int totalNumberOfRecords;

如果您完全确定不会同时从多个线程调用您的方法,您可以将totalNumberOfRecords作为一个静态字段移动到方法之外。

好的,那么问题出在哪里呢?通过ajax发布此服务method@suganya,然后如回答中所述,您可以使用
会话
Cache
。有关更多信息,请参见MSDN。确实吗?OP说这是一个web应用程序。现在您有什么建议吗?
int totalNumberOfRecords=HttpContext.Current.Session[“totalNumberOfRecords”]?(int)HttpContext.Current.Session[“totalNumberOfRecords”]:0;
并在计数时保存totalNumberOfRecords。
totalNumberOfRecords=HttpContext.Current.Session[“totalNumberOfRecords”]=GetCountOfPlantAttributes(searchColumn,searchValue);