Page 1 of 1

Batch processing issue

Posted: Tue 29 Jun 2021 16:55
by FlorianDahn
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

Re: Batch processing issue

Posted: Wed 30 Jun 2021 13:23
by FlorianDahn
So what I have done for now looks like this:

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());

}
When you have better ideas, please let me know.
Thanks

Re: Batch processing issue

Posted: Mon 05 Jul 2021 07:37
by Shalex
You have found the only way of accessing inserted records before calling SubmitChanges().

Re: Batch processing issue

Posted: Thu 08 Jul 2021 01:01
by FlorianDahn
Ok thank you. Good to know.

Re: Batch processing issue

Posted: Thu 08 Jul 2021 12:48
by FlorianDahn
For everbody else having the same "issue".
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;
}