Is there a proper way to deal with Out Of Memory Exceptions while looping through a large list and adding objects from that list? What is the proper way to go about doing this? I have a large Linq query that returns around 600K items. I then go through each item and add it to an object. The code is below.
static void Main(string[] args)
{
GrabData();
}
public static void GrabData()
{
decimal? TotalNetClaim = 0;
using (var Context = new VSCMeSnapEntities())
{
List<DriveTimeObject> DataFile = new List<DriveTimeObject>();
DriveTimeObject DT = new DriveTimeObject();
DateTime ParamDate = new DateTime(2015, 05, 30);
List<viewDriveTimeFileDump> DataQuery = new List<viewDriveTimeFileDump>();
DataQuery = (from z in Context.viewDriveTimeFileDumps select z).ToList();
foreach (var item in DataQuery)
{
decimal? AmountChargedParts = DT.GetAmountChargedParts(item.chrComponSts.Trim(), item.mnsTotalParts);
decimal? AmountChargedPartsTax = DT.GetAmountChargedPartsTax(item.chrComponSts.Trim(), item.mnsTotalPartTax);
decimal? AmountChargedLabor = DT.GetAmountChargedLabor(item.chrComponSts.Trim(), item.mnsTotalLabor);
decimal? AmountChargedLaborTax = DT.GetAmountChargedLaborTax(item.chrComponSts.Trim(), item.mnsTotalLaborTax);
int? DaysOut = DT.GetDaysOutClaim(item.intRepairFacilCode, item.dtmContPurchDate, item.dtmReported);
long? MilesOut = DT.GetMilesOutClaim(item.intRepairFacilCode, item.inbIncurMiles, item.inbOrigMiles);
decimal? deductible = DT.GetDeductible(item.chrContSts, item.mnsDeduct);
decimal? netClaim = DT.GetNetClaim(item.chrComponSts.Trim(), item.mnsTotalParts, item.mnsTotalPartTax, item.mnsTotalLabor, item.mnsTotalLaborTax, item.mnsDeduct);
DataFile.Add(new DriveTimeObject
{
DealerNumber = item.chrDlrNum,
VSCName = item.chvVSCName,
IcLocationNumber = item.IcLocationNumber,
IcRegion = item.IcRegion,
Identifier = item.chrIdentifier,
ContractNumber = item.chrContNum,
VIN = item.chrVIN,
CoverageCode = item.CvgCode,
ClaimNum = item.intClaimNum,
OriginalMiles = item.inbOrigMiles,
ContractPurchaseDate = item.dtmContPurchDate,
IncurMiles = item.inbIncurMiles,
DateReported = item.dtmReported,
DaysOutClaim = DaysOut,
MilesOut = MilesOut,
RepairFacilityNumber = item.intRepairFacilCode,
FacilityName = item.chvFacilityName,
ZipFive = item.chrZipFive,
FacilityAdvisor = item.chrFacilAdvisor,
ComponentStatus = item.chrComponSts,
ComponentStatusWord = item.ComponDesc,
ComponentCode = item.chrComponCode,
StatusMasterDescription = item.MasterDesc,
ComponentDescription = item.chvComponDesc,
Parts = AmountChargedParts,
PartsTax = AmountChargedPartsTax,
Labor = AmountChargedLabor,
LaborTax = AmountChargedLaborTax,
Deductible = deductible,
NetClaim = netClaim,
CarrierCode = item.intCarrierCode,
NetworkStatus = item.NetworkStatus,
AddOn = item.chrAddOn,
ETCDate = item.ETC,
ATCDate = item.ATC,
LaborTime = item.reaLaborTime,
PaidDate = item.dtmPdDate,
PaymentID = item.intPaymentID,
BatchNumber = item.intBatchNum
});
TotalNetClaim += netClaim;
}
Context.Dispose();
}
Console.WriteLine(TotalNetClaim);
Console.ReadKey();
}
I run out of memory during the foreach loop and I was wondering how I should go about adjusting my code to make this work.
DataFileList? You keep lots of objects in it but never use it...