I have list like this:
var list = new List<string>();
This list contains lets say following names: “Ken”, “John”, “Tom”, etc.
I need to add this list to database table which looks like this:
Id SecondId Name
1 1 Ken
2 1 John
3 2 Tom
Where SecondId is secondary key and that info i already have. But my question is there a better way of adding all names to the database without iterating through list using foreach loop? Any linq query or some other way or i have to iterate through the list to add them to database one by one?
I don’t think there is a way to add the list as a chunk.
You could use the
ForEachextension method on the List like so (assuming you are using aDbContextfor database access):You can use the same approach but use you database writing code inside the lambda expression in case you are not using DbContext.