Home > Net >  How to access a LINQ Query Object from the .cshtml file back end in a class file
How to access a LINQ Query Object from the .cshtml file back end in a class file

Time:10-24

I want to access the logs LINQ Query Object that is queried in the messageLogs.cshtml file in the ReportingConcrete class File

This logs LINQ Query is the object search from the Razor GridView that is queried(in the messageLogs.cshtml) and I want it to be parsed to the ReportingConcrete class so that it outputs the excel report based on the queried object.

To summarise this, I want to export the excel report only for the fields searched in the Razor GridView.(If there is any Search at all).

If there is no search in the Gridview, the Excel report should be generated from the entire database's details.(I already have this).

ReportingConcrete.cs

messageLogs.cshtml.cs

 try
        {
            //var logsList = await context.MessageArchive.FindAsync();
            var logs = await context.MessageArchive
                .Include(x => x.Conversation).ToListAsync();

            if (!string.IsNullOrEmpty(Request.Form["number"])) //If only number entered
            {
                NumberSearched = Convert.ToString(Request.Form["number"]);
                logs = logs.Where(x => x.PhoneNumber == NumberSearched).ToList(); //Gathers all results from thelogs
                //logsList = logs.Where(x => x.PhoneNumber == NumberSearched).to;
            }

            else if (!string.IsNullOrEmpty(Request.Form["fromDate"])) //If fromDate entered
            {
                fromDate = Convert.ToDateTime(Request.Form["fromDate"]);
                logs = logs.Where(x => x.MessageDate >= fromDate).ToList(); //Gathers all results from the fromDate

                if (!string.IsNullOrEmpty(Request.Form["toDate"])) //if form has toDate. Then logs list will include toDate parameters 
                {
                    toDate = Convert.ToDateTime(Request.Form["toDate"]);
                    logs = logs.Where(x => x.MessageDate <= toDate).ToList();
                }
            }

            else if (!string.IsNullOrEmpty(Request.Form["toDate"])) //If toDate entered
            {
                toDate = Convert.ToDateTime(Request.Form["toDate"]);
                logs = logs.Where(x => x.MessageDate <= toDate).ToList(); //Gathers all results from the toDate
            }
       

CodePudding user response:

Parse the Object list in the cshtml.cs to a Class Then retrieve and access this class in the ReportingConcrete class.

` ArchiveExcelClass archiveExcelClass = new ArchiveExcelClass();

            var listofusers = new ArchiveExcelClass
            {
                ArchiveExcel = logs,  //.ToList()

                //ConversationId = logs.,
                //PhoneNumber = logsList.PhoneNumber,
                //MessageDate = archiveExcelViewModel.MessageDate,
                //Status = archiveExcelViewModel.Status,
                //Body = archiveExcelViewModel.Body,

            };
            return listofusers;`

 var gridSearch = _archiveExcelClass.ArchiveExcel;

        if (gridSearch != null)
        {
            foreach (var item in gridSearch)
            {
                var listofusers = (from ArchiveExcelClass in _archiveExcelClass.ArchiveExcel
                                   //_databaseContext.MessageArchive.AsNoTracking()
                                   /*var listofusers =*/
                                   select new userViewModel
                                   {
                                       ConversationId = item.Id.ToString(),
                                       PhoneNumber = item.PhoneNumber,
                                       MessageDate = item.MessageDate,
                                       Status = item.Status.ToString(),
                                       Body = item.Body,
                                   });

                return (List<userViewModel>)listofusers;   // return listofusers;
            }
  • Related