Filtering by array field ends with an error

I have a screen with an array field, but when I try to filter by this field I end with an error. The architecture of the data is rather complicated. The parent entity is RequiredPayment. It contains the array field RealEstate based on RequiredPaymentDetail.refRealEstateId. The data structure contains the child entity RequiredPaymentDetail with all fields and then the entity RequiredPaymentDetail_Filter with child entity RealEstate used for filtering by the logged in user (relation type FilterParent). Both child entities use the same relation to the parent entity. As far as I can tell it looks as if the array field is trying to look for the data in the filter child entity. Is this bad modelling or is the error caused by issues when generating the SQL query?

2026-06-05 14:18:17,695 [5] ERROR Origam.Server.Controller.AbstractController - The multi-part identifier "RequiredPaymentDetail_Filter.refRequiredPaymentId" could not be bound.
The multi-part identifier "RequiredPaymentDetail_Filter.refRealEstateId" could not be bound.
System.Data.SqlClient.SqlException (0x80131904): The multi-part identifier "RequiredPaymentDetail_Filter.refRequiredPaymentId" could not be bound.
The multi-part identifier "RequiredPaymentDetail_Filter.refRealEstateId" could not be bound.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
   at StackExchange.Profiling.Data.ProfiledDbCommand.ExecuteDbDataReader(CommandBehavior behavior) in C:\Users\jindr\Repos\mini-profiler\src\MiniProfiler.Shared\Data\ProfiledDbCommand.cs:line 206
   at Origam.DA.Service.AbstractSqlDataService.ExecuteDataReader(DataStructureQuery query, IPrincipal principal, String transactionId) in D:\a\1\s\backend\Origam.DA.Service\AbstractSqlDataService.cs:line 1848
   at Origam.DA.Service.AbstractSqlDataService.ExecuteDataReaderInternal(DataStructureQuery query)+MoveNext()
   at System.Linq.Enumerable.SelectEnumerableIterator`2.ToList()
   at Origam.Server.Controller.AbstractController.ExecuteDataReader(DataStructureQuery dataStructureQuery, Guid methodId) in D:\a\1\s\backend\Origam.Server\Controller\AbstractController.cs:line 536
   at Origam.Server.Controller.UIServiceController.<>c__DisplayClass36_0.<GetRows>b__3(DataStructureQuery dataStructureQuery) in D:\a\1\s\backend\Origam.Server\Controller\UIServiceController.cs:line 403
   at CSharpFunctionalExtensions.ResultExtensions.Bind[T,K,E](Result`2 result, Func`2 func)
   at Origam.Server.Controller.UIServiceController.<>c__DisplayClass36_0.<GetRows>b__0() in D:\a\1\s\backend\Origam.Server\Controller\UIServiceController.cs:line 400
   at Origam.Server.Controller.AbstractController.<>c__DisplayClass9_0.<RunWithErrorHandler>g__AsynFunc|0() in D:\a\1\s\backend\Origam.Server\Controller\AbstractController.cs:line 98
   at Origam.Server.Controller.AbstractController.RunWithErrorHandlerAsync(Func`1 func) in D:\a\1\s\backend\Origam.Server\Controller\AbstractController.cs:line 117
ClientConnectionId:028ffad2-dae0-4a9b-9dba-40f0b06807c5
Error Number:4104,State:1,Class:16

The issue is the construction of the filter request. The second part of the filter that selects data based on the refRealEstateId doesn’t correctly construct the alias RequiredPaymentDetail_Filter that is used in the WHEREclause. Or more correctly I think the second EXISTS should be referencing the full RequiredPaymentDetail entity, not the filtering one, so the part _Filter should not be in the last WHERE clause.

SELECT COUNT_BIG([RequiredPayment].[Id]) as IdCount  FROM [RequiredPayment] AS [RequiredPayment] 
	WHERE  EXISTS (
		SELECT * FROM [RequiredPaymentDetail] AS [RequiredPaymentDetail_Filter] 
		INNER JOIN [RealEstate] AS [RealEstate] 
			ON [RequiredPaymentDetail_Filter].[refRealEstateId] = [RealEstate].[Id]  
			AND  (  (( ([RealEstate].[refBusinessPartnerId] = 'xxx') 
					OR  ([RealEstate].[refBusinessPartnerId2] = 'xxx')))) 
			WHERE  [RequiredPayment].[Id] = [RequiredPaymentDetail_Filter].[refRequiredPaymentId]) 
	AND EXISTS(
		SELECT * FROM [RequiredPaymentDetail] 
			WHERE [RequiredPayment].[Id] = [RequiredPaymentDetail_Filter].[refRequiredPaymentId] 
			AND [RequiredPaymentDetail_Filter].[refRealEstateId] IN ('xxx'))

So this is the problem – the sql generator selects the wrong entity to render the WHERE part. It should have been RequiredPaymentDetail as correctly stated in the FROM part.