I have an entity type, let’s call it Transaction, created by entity framework version 1. I have added the following property:
public partial class Transaction
{
public int CurrentMaxRevisionNumber { get; set; }
}
And I have the following query:
public List<Transaction> GetTransactions(DateTime startDate, DateTime endDate)
{
var query = from t in Repository.Transaction
let currentMaxRevisionNumber =
Repository.Transaction.Where(t1 => t1.TransactionId == t.TransactionId)
.Max(t1 => t.RevisionNumber)
where t.StartDate >= startDate
&& t.EndDate < endDate)
select t;
return query.ToList();
}
The business rules for what we are trying to accomplish are the following:
– A new record for the transaction on the original date should be created with a code that indicates it has been soft deleted with an increased revision number.
– A new record for the transaction is to be created on the new date with an increased revision number.
The problem is that the process that determines when the transaction will be processed is done sequentially one day at a time and transactions are being moved from date to date and sometimes back to their original ones constantly. This may result that the max revision number of a given transaction will be the one from the specific date and not the max for the transaction. That is the reason why I am trying to do the “let” in the query.
Now the questions:
– How can I populate the CurrentMaxRevisionNumber property I created in the partial class with the query?
– Is there maybe another way I can accomplish this without having to do a query for each transaction since there may be several hundreds at any given date?
Thanks for you help.
So the first thing I tried was to use the anti-pattern SELECT n + 1, which is a horrible solution but at least that showed us that by getting the correct data the problem would be fixed. It was something like:
For obvious reasons this solution was not acceptable so we came up with the following:
By using the anonymous object we can get all the data that we need in a single database access and the use the loop to populate the missing property.
Since the liberty we had to modify the database and the application was somewhat limited, this solution proved to be the least invasive and more performant.