473,473 Members | 1,484 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Avoid instatiation inside foreach loop

1 New Member
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.         }
May 15 '19 #1
0 1419

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

Similar topics

13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
18
by: Ken Varn | last post by:
Is there any way to reset a foreach loop to re-iterate through the collection as if it were starting from the beginning? Namely, if I delete an item out of a collection, I want to be able to reset...
2
by: dSchwartz | last post by:
I need help adding a column to a dataset, but its a little bit more complicated then just that. Here's the situation: I have many xml files in one directory, each which represent a newsletter. I...
2
by: ad | last post by:
I want to delete a DataRowView in a DataView if the DataRowView not checked OK. (CheckRow is a function for checking ) I used the codes below: But when some row is delete, it fail , the error...
2
by: Alan Silver | last post by:
Hello, I have code like the following... foreach (Control ctl in Page.Controls) { if (ctl.ID.StartsWith("X_")) { // do stuff } }
10
by: =?Utf-8?B?YmJn?= | last post by:
Hi all, I wanted to go through each entry(?) of ArrayList and remove some particular entry. So I tried following but it throws exception at runtime: foreach (myEntry entry in myArrayList) {...
23
by: tshad | last post by:
Is there a way to know if you are looking at the last record record of foreach loop other then setting up a loop counter that you manually increment? foreach (Racecar racecar in...
1
by: bronso | last post by:
I can`t get this block of code to work. I think there is something wrong with my syntax, but I can`t figure out what it is. source code look like this: $stemmer=array( ...
8
by: Andrus | last post by:
Code below causes error in class definition line .....Isolator<T>' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'Myapp.Isolator<T>.GetEnumerator()'...
1
by: Shepard | last post by:
Hi, I have been learning JSP and Servlets when I came upon JSTL and EL concepts.. I am new to JSP... From what I understand JSTL and EL was introduced in JSP so as to avoid using scriplets<%%> in...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.