I need to improve the performance of my insert in my C# application. I first go out and get data from a view. Then I go through a FOREACH loop to insert into a table. I have over 200,000 records that I am working with and it takes an ridiculous amount of time to perform this task. I know the SaveChanges is a round trip to the database but I'm not sure how to get around this. Is there something I can do to improve the time?
var values = db.TodaysAirs.ToList();
foreach (TodaysAir x in values)
{
//check to see if this is a new value or one that needs to be updated
var checkForNew = db.TodaysAirValues
.Where(m => m.ID == x.ID);
//new record
if (checkForNew.Count() == 0)
{
TodaysAirValue newRecord = new TodaysAirValue();
newRecord.ID = x.ID;
newRecord.Logger_Id = x.Logger_Id;
newRecord.SiteName = x.SiteName;
newRecord.Latitude = x.Latitude;
newRecord.Longitude = x.Longitude;
newRecord.Hour = x.Hour;
newRecord.Parameter = x.Parameter;
newRecord.Stan = x.Stan;
newRecord.Units = x.Units;
newRecord.InstrumentType = x.InstrumentType;
newRecord.NowCast = x.NowCast;
newRecord.AQIValue = x.AQIValue;
newRecord.HealthCategory = x.HealthCategory;
newRecord.Hr24Avg = x.Hr24Avg;
newRecord.Hr24Max = x.Hr24Max;
newRecord.Hr24Min = x.Hr24Min;
newRecord.SID = DateTime.Now;
db.TodaysAirValues.Add(newRecord);
db.SaveChanges();
// CallJenkinsJob();
}
}
.Count() == 0withAny()and get the same result.