Hey together,
I'm working actually on a tool for importing product lists (csv and xml) into a NopCommerce shop.
I'm making "batches" (100 iterations over InsertOnSubmit) and then calling the SubmitChanges method.
When importing Brands the name itself will appear multiple times (for sure) but when using the FirstOrDefault method on the specific table, even it has been added before with InsertOnSubmit (No SubmitChanges call yet) I will receive null.
Is this common? Or is there a simple configuration change I have to do?
I'm remembering that on the original LinqToSql it will work even of not yet called SubmitChanges. Id will be 0 but thats as expected.
Devart.Data: 5.0.2522.0
Devart.Data.Linq: 4.9.2067.0
Thanks in advance!
Warm regards
Florian Dahn
Batch processing issue
-
- Posts: 26
- Joined: Thu 06 Feb 2020 18:23
Re: Batch processing issue
So what I have done for now looks like this:
When you have better ideas, please let me know.
Thanks
Code: Select all
//Try to find them the "normal" way
var manufacturer = db.Manufacturers.FirstOrDefault(a => a.Name.ToLower() == value.ToLower());
//Fix for not founding previously created instances
if (manufacturer == null)
{
manufacturer = db.GetChangeSet().Inserts.Where(a => a.GetType() == typeof(Manufacturer))
.Select(a => a as Manufacturer)
.FirstOrDefault(a => a.Name.ToLower() == value.ToLower());
}
Thanks
Re: Batch processing issue
You have found the only way of accessing inserted records before calling SubmitChanges().
-
- Posts: 26
- Joined: Thu 06 Feb 2020 18:23
Re: Batch processing issue
Ok thank you. Good to know.
-
- Posts: 26
- Joined: Thu 06 Feb 2020 18:23
Re: Batch processing issue
For everbody else having the same "issue".
I build some generic extension methods which will help here "automatically".
I build some generic extension methods which will help here "automatically".
Code: Select all
public static List<T> WhereOrInserted<T>(this Table<T> table, Expression<Func<T, bool>> predicate)
where T : class
{
var result = table.Where(predicate).ToList();
if (result != null && result.Count != 0)
return result;
result = table.Context.GetChangeSet().Inserts.Where(a => a.GetType() == typeof(T))
.Select(a => a as T)
.ToList();
return result;
}
public static T FirstOrInserted<T>(this Table<T> table, Expression<Func<T, bool>> predicate)
where T : class
{
var result = table.First(predicate);
if (result != null)
return result;
result = table.Context.GetChangeSet().Inserts.Where(a => a.GetType() == typeof(T))
.Select(a => a as T)
.First();
return result;
}
public static T FirstOrDefaultOrInserted<T>(this Table<T> table, Expression<Func<T, bool>> predicate)
where T : class
{
var result = table.FirstOrDefault(predicate);
if (result != null)
return result;
result = table.Context.GetChangeSet().Inserts.Where(a => a.GetType() == typeof(T))
.Select(a => a as T)
.FirstOrDefault();
return result;
}