Batch processing issue

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
FlorianDahn
Posts: 26
Joined: Thu 06 Feb 2020 18:23

Batch processing issue

Post by FlorianDahn » Tue 29 Jun 2021 16:55

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

FlorianDahn
Posts: 26
Joined: Thu 06 Feb 2020 18:23

Re: Batch processing issue

Post by FlorianDahn » Wed 30 Jun 2021 13:23

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

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Batch processing issue

Post by Shalex » Mon 05 Jul 2021 07:37

You have found the only way of accessing inserted records before calling SubmitChanges().

FlorianDahn
Posts: 26
Joined: Thu 06 Feb 2020 18:23

Re: Batch processing issue

Post by FlorianDahn » Thu 08 Jul 2021 01:01

Ok thank you. Good to know.

FlorianDahn
Posts: 26
Joined: Thu 06 Feb 2020 18:23

Re: Batch processing issue

Post by FlorianDahn » Thu 08 Jul 2021 12:48

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;
}

Post Reply