c# – Copy vehicle information from JSON list to relational database

jsonProduct("VehicleApplications").Count() should be outside the iteration, otherwise it will be executed on each iteration which impact the performance. So, you should do :

var count = jsonProduct("VehicleApplications").Count();

for (int i = 0; i < count; i++)
{
    var vehicleApp = jsonProduct("VehicleApplications")(i);
    ///...... etc.
}

using SingleOrDefault is slower than FirstOrDefault and that’s because of their behavior. SingleOrDefault will recheck the enumerable against the founded item, to see if there is any duplicates, while FirstOrDefault doesn’t do this check. In your case, you don’t need to check the enumerable, you need to get the first item that matches the condition. So, using FirstOrDefault would be better choice.

also, when you use HashSet you don’t really need to recheck the set for duplicates when adding items, as the HashSet will take care of that, and will always store unique items. So, if you try to add an item that is already exists in the HashSet, it won’t be added nor change the existing one.

try this version and see how it perform :

var vehicleApplications = jsonProduct("VehicleApplications"); 

if(vehicleApplications != null)
{
    HashSet<VehicleApplication> vehicleApplicationHashSet = new HashSet<VehicleApplication>();
    
    List<ProductVehicleApplication> productVehicleApplicationList = new List<ProductVehicleApplication>();

    var count = vehicleApplications.Count();

    for (int i = 0; i < count; i++)
    {
        var vehicleApp = jsonProduct("VehicleApplications")(i);

        VehicleApplication vehicleApplication = new VehicleApplication
        {
            CategoryName = vehicleApp("CategoryName").ToString(),
            MakeName = vehicleApp("MakeName").ToString(),
            MfrLabel = vehicleApp("MfrLabel").ToString(),
            ModelName = vehicleApp("ModelName").ToString(),
            Note = vehicleApp("Note").ToString(),
            PartTypeName = vehicleApp("PartTypeName").ToString(),
            PositionName = vehicleApp("PositionName").ToString(),
            Qty = vehicleApp("Qty").ToString(),
            SubCategoryName = vehicleApp("SubCategoryName").ToString(),
            YearID = vehicleApp("YearID").ToString()
        };
        
        var existingVehicleApplication = dbVehiclesList.FirstOrDefault(x =>
               x.MakeName == vehicleApplication.MakeName 
            && x.ModelName == vehicleApplication.ModelName 
            && x.CategoryName == vehicleApplication.CategoryName
            && x.MfrLabel == vehicleApplication.MfrLabel
            && x.PartTypeName == vehicleApplication.PartTypeName
            && x.PositionName == vehicleApplication.PositionName
            && x.Qty == vehicleApplication.Qty
            && x.YearID == vehicleApplication.YearID
            && x.SubCategoryName == vehicleApplication.SubCategoryName); 
        
        if(existingVehicleApplication != null)
        {
            vehicleApplication = existingVehicleApplication;
        }
        else
        {
            // if the vehicleApp is already in the hashset, then it'll not be added.
            vehicleApplicationHashSet.Add(vehicleApplication);
        }
        
        ProductVehicleApplication productVehicleApplication = new ProductVehicleApplication
        {
            Product = dbProductList.FirstOrDefault(x=>x.Id==product.Id),
            VehicleApplication = vehicleApplication
        };
        
        productVehicleApplicationList.Add(productVehicleApplication);
    }

    dbObj.BulkInsert(productVehicleApplicationList);
    dbObj.BulkInsert(vehicleApplicationList);
    dbObj.SaveChanges();
}