Scott Harrison - The golden apples
Henry Parker's Robot Wars

ASP.Net, ADO, MS Access
C# How to IQueryable all the way down

C# How to IQueryable all the way down

(16 hits)
Overview: Hits=11 This snippet demonstrates how to use IQueryable to return data to a module method. IQueryable is an IEnumerable derived class meaning it allows you to foreach items in the collection. IQueryable does not retrieve data from the database until it is being enumerated. This allows you to retrieve large datasets and process the items in the dataset. IQueryable does not have an awaiter, so making it async is possible but not by design. To make it async you would convert the query to a list collection by type and return the list as AsQueryable.

Hits=5 The disadvantage of Async all the way down with IQueryable is you use memory resource to build the collections. If the collections are large than your server resource is consumed.

ToListAsync

    public async Task<IQueryable<ProjectManagementMilestone>> GetTasksByMilestoneId(long milestoneId)
        {
            try
            {
                var list = await base.GetObjectsAsync(e => e.MilestoneId == milestoneId, "ProjectManagementTasks").ToListAsync();

                return list.AsQueryable<ProjectManagementMilestone>();
	    }
	    catch (Exception ex)
		{throw new Exception(GetMyMethodName(), ex);}
}

IQueryable all the way down, no async

Unit Test

 [Fact]
        public void TestGetTasksByMilestoneId()
        {
            long milestoneId = 1;
            ProjectManagementModule pmMod = new ProjectManagementModule();

            IQueryable<ProjectManagementMilestone> query =  pmMod.GetTasksByMilestoneId(milestoneId);

            int count = 0;
            foreach (var item in query)
            {
                foreach (var task in item.ProjectManagementTasks)
                {
                    output.WriteLine($"{task.TaskName}");
                    count++;
                }
            }
            Assert.True(count > 0);
        }

ProjectManagement Module

 public IQueryable<ProjectManagementMilestone> GetTasksByMilestoneId(long milestoneId)
        {
            try
            { 
            IQueryable<ProjectManagementMilestone> query = unitOfWork.projectManagementMilestoneRepository.GetTasksByMilestoneId(milestoneId);
            return query;
            }
            catch (Exception ex)
            {
                throw new Exception(GetMyMethodName(), ex);
            }
        }
 

projectManagementProjectRepository

public IQueryable<ProjectManagementMilestone> GetTasksByMilestoneId(long milestoneId)
        {
            try
            {
                //var list = await base.GetObjectsAsync(e => e.MilestoneId == milestoneId, "ProjectManagementTasks").ToListAsync();

                //return list.AsQueryable<ProjectManagementMilestone>();
                IQueryable<ProjectManagementMilestone> query =  base.GetObjectsQueryable(e => e.MilestoneId == milestoneId, "ProjectManagementTasks");
                return query;
            }
            catch (Exception ex) { throw new Exception(GetMyMethodName(), ex); }

        }

Repository

public IQueryable<T> GetObjectsQueryable(Expression<Func<T, bool>> predicate,string includeTable="") { IQueryable<T> result = _dbContext.Set<T>().Where(predicate); if (includeTable != "") { result=result.Include(includeTable); } return result; }

Otherwise Async all the way down


  [Fact]
        public async Task TestGetTasksByMilestoneId()
        {
            long milestoneId = 1;
            ProjectManagementModule pmMod = new ProjectManagementModule();

            IQueryable<ProjectManagementMilestone> query = await pmMod.GetTasksByMilestoneId(milestoneId);

            int count = 0;
            foreach (var item in query)
            {
                foreach (var task in item.ProjectManagementTasks)
                {
                    output.WriteLine($"{task.TaskName}");
                    count++;
                }
            }
            Assert.True(count > 0);
        }

public async Task<IQueryable<ProjectManagementMilestone>> GetTasksByMilestoneId(long milestoneId)
        {
            try
            { 
            IQueryable<ProjectManagementMilestone> query = await unitOfWork.projectManagementMilestoneRepository.GetTasksByMilestoneId(milestoneId);
            return query;
            }
            catch (Exception ex)
            {
                throw new Exception(GetMyMethodName(), ex);
            }
        }

   public async Task<IQueryable<ProjectManagementMilestone>> GetTasksByMilestoneId(long milestoneId)
        {
            try
            {
                var list = await base.GetObjectsAsync(e => e.MilestoneId == milestoneId, "ProjectManagementTasks").ToListAsync();

                return list.AsQueryable<ProjectManagementMilestone>();
          
            }
            catch (Exception ex) { throw new Exception(GetMyMethodName(), ex); }

        }

   public IQueryable<T> GetObjectsAsync(Expression<Func<T, bool>> predicate,string includeTable="")
        {
            IQueryable<T> result = _dbContext.Set<T>().Where(predicate);
            if (includeTable != "")
            {
                result=result.Include(includeTable);
            }
                
            
                return result;
        }


....

...<<<Register to correspond>>> ...

Members : 152
Name:
Email:

Register to View

Help