Reputation: 1386
I have a big Table , about 1 million records , and it's increasing .....
Now i want retrieve 1K records to a list (List) and do something with it and save results to 3 table (use transaction ) , when these 1k records are handled , how can i know and retrieve again ....
i used Ado.net entity framework as data access module .
I want to use MultiThreading , but i am not very familiar with it. how can i get this done .
could you give me some sample code or Pseudocode ?
Upvotes: 1
Views: 451
Reputation: 59915
If you're on .NET 4.0 I would recommend you using the Task Parallel Library to perform any sort of parallel operations.
Here's an example of how you could use it in your scenario:
IEnumerable<Entity> entities;
int rangeStartIndex = 0;
int rangeLength = 1000;
do
{
entities = dataContext.Entities
.Skip(rangeStartIndex)
.Take(rangeLength);
Parallel.ForEach(
entities,
item =>
{
// Process a single entity in the list
});
using (var tran = new TransactionScope())
{
// Persist the entities in the database
tran.Complete();
}
rangeStartIndex += rangeLength;
} while (entities.Any());
Here we are processing the list of entities in a parallel fashion using the Parallel.ForEach method, without having to worry about managing threads.
Related resources:
Upvotes: 3
Reputation: 3621
If you're using entity framework, why not declare methods in your repository layer like:
public IEnumerable<Order> GetOrders(int skip, int take)
{
return _context.Orders.Skip(skip).Take(take).AsEnumerable();
}
public IEnumerable<Order> GetOrders(Expression<Func<Order, bool>> predicate, int skip, int take)
{
return _context.Orders.Where(predicate).Skip(skip).Take(take).AsEnumerable();
}
You can then call the required method in a loop to pull back blocks of data and since the IQueryable isn't executed until .AsEnumerable() is called, you'll get an efficient paging mechanism.
Obivously this solution isn't multi-threaded but you could use it as the basis of your threaded looping construct as long as you ensure that your counter variables are marked as volatile.
Upvotes: 0
Reputation: 35895
You can use Concurrent Collections. In this particular case you can try ConcurrentBag.
Upvotes: 0