Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to get number of rows in a table without using count function?
I Am use Count but its get too much time to load data so what is another way to load data without count..I am use count because find all user which one already assign services or not assign services.

What I have tried:

public IActionResult Index()
        
        {
            List<ServiceUserVM> seruser = new List<ServiceUserVM>();
            var alluser = this.cc.users.ToList();
            var allservices = this.cc.Services.ToList();

            for(int i=0;i<alluser.Count();i++)
            {
             
                for (int j = 0; j < allservices.Count(); j++)
                {
                    ServiceUserVM serviceUser = new ServiceUserVM();
                    if (this.cc.serviceAuthorizations.Any(p => p.UserID == alluser[i].UserID && p.ServiceID == allservices[j].ServiceID))
                    {
                       
                        serviceUser.UserID = alluser[i].UserID;
                        serviceUser.UserName = alluser[i].UserName;
                        serviceUser.ServiceName = allservices[j].ServiceName;
                        serviceUser.ServiceID = allservices[j].ServiceID;
                        serviceUser.status = "Allready Exist";
                        seruser.Add(serviceUser);
                    }
                    else
                    {


                        serviceUser.UserID = alluser[i].UserID;
                        serviceUser.UserName = alluser[i].UserName;
                        serviceUser.ServiceName = allservices[j].ServiceName;
                        serviceUser.ServiceID = allservices[j].ServiceID;
                        serviceUser.status = "Services Not Exist";
                        seruser.Add(serviceUser);
                    }
                }
            }

            return View(seruser);
        }
Posted
Updated 8-Feb-23 17:40pm
v3

It's not the call to .Count that's slowing it down. You've got two lines that appear to be loading entire tables, users and Services, then you iterate over the both both tables, the Services table every single time you look at the next user in the users table. You have a loop nested inside a loop and that's what killing your performance.

Next, you're executing this statement (Num user records * Num Services records) times:
C#
this.cc.serviceAuthorizations.Any(p => p.UserID == alluser[i].UserID && p.ServiceID == allservices[j].ServiceID

That's going to take a while, PER ITERATION THROUGH THE LOOP.

I don't know what you're trying to do with this code, because I'm too damn tired right now to think, but it seems like this would be a solution better solved in the database and not iterating through a bunch of rows doing it in inefficient code.
 
Share this answer
 
I don't think it is getting the count that is slowing you down.
I believe it is the for loop.

Comment out the for loop and try again.
C#
public IActionResult Index()
        
        {
            List<ServiceUserVM> seruser = new List<ServiceUserVM>();
            var alluser = this.cc.users.ToList();
            var allservices = this.cc.Services.ToList();

            Console.WriteLine(allservices.Count());
            Console.WriteLine(alluser.Count());
            
            return View(seruser);
        }


If that is faster then it is simply the fact that the for loop is looping over a large number of records.
 
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