By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Avoid instatiation inside foreach loop

P: 1
how to avoid this violation

I don't want to instantiate gpsData inside loop.So how to change this code to achieve this functionality?

Expand|Select|Wrap|Line Numbers
  1. public List<MODELS.GPSRequest> GetGPSDataQueuedStatus()
  2.         {
  3.             var gpsRequestList = new List<MODELS.GPSRequest>();
  4.  
  5.             var gpsList = _unitofwork.GpsRequests.Get(x => x.GpsRequestStatusId == 1).Select(x => x.GpsRequestId).ToList();
  6.  
  7.             if (gpsList.Any())
  8.             {
  9.  foreach (var gps in gpsList.OrderBy(x => x.GpsRequestId))
  10.                 {
  11.                     var gpsData = new MODELS.GPSRequest();
  12.                 gpsData.GpsRequestId = gps.GpsRequestId;
  13.                 gpsRequestList.Add(gpsData);    
  14.                 }
  15.             }
  16.             return gpsRequestList;
  17.         }
1 Week Ago #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.