Hi, I am trying to create a report using a TypedView.
The view looks like this.
SELECT dl.Name, rd.DoctorInvolvementTypeId, r.DateTimeRequested
FROM Request_DoctorLocation rd, DoctorLocation dl, Request r
WHERE r.RequestId = rd.RequestId and dl.DoctorLocationId = rd.DoctorLocationId
I need to add an additional section through predicates to this:
AND (SELECT COUNT(sdl.Name)
FROM Request_DoctorLocation srd, DoctorLocation sdl, Request sr
WHERE sr.RequestId = srd.RequestId and sdl.DoctorLocationId = srd.DoctorLocationId and sdl.Name = dl.Name and sr.DateTimeRequested
between '2007-01-20 14:09:00.000' and '2007-05-23 14:09:00.000' )> some value
When i run this , it works fine but I cannot create it in my code.
Here is my attempt
IPredicateExpression predicateExpression = new PredicateExpression(
new FieldBetweenPredicate(ViewDoctorRequestsFields.DateTimeRequested, e.DateFrom, e.DateTo));
predicateExpression.Add(ViewDoctorRequestsFields.DoctorInvolvementTypeId == DoctorInvolvementTypeEntity.Primary);
PredicateExpression totalPredicateExpression = new PredicateExpression(new FieldBetweenPredicate(ViewDoctorRequestsFields.DateTimeRequested, e.DateFrom, e.DateTo));
RelationCollection relationCollection = new RelationCollection();
relationCollection.Add(RequestEntity.Relations.RequestDoctorLocationEntityUsingRequestId);
relationCollection.Add(RequestDoctorLocationEntity.Relations.DoctorLocationEntityUsingDoctorLocationId);
// JF van Zyl: Create a field to return the total tests for the request
IEntityField totalCount = new EntityField("TotalCount",
new ScalarQueryExpression(ViewDoctorRequestsFields.Name.SetAggregateFunction(AggregateFunction.Count),
totalPredicateExpression, relationCollection));
predicateExpression.Add(new FieldCompareValuePredicate(totalCount, ComparisonOperator.GreaterThan, CustomConvert.ToInt32(txtMinValue.Text, 0)));
It generates the following sql, which is wrong,
exec sp_executesql N'SELECT [preLink].[dbo].[viewDoctorRequests].[Name], [preLink].[dbo].[viewDoctorRequests].[DoctorInvolvementTypeId], [preLink].[dbo].[viewDoctorRequests].[DateTimeRequested] FROM [preLink].[dbo].[viewDoctorRequests] WHERE ( ( ( [preLink].[dbo].[viewDoctorRequests].[DateTimeRequested] BETWEEN @DateTimeRequested1 AND @DateTimeRequested2 AND [preLink].[dbo].[viewDoctorRequests].[DoctorInvolvementTypeId] = @DoctorInvolvementTypeId3 AND (SELECT TOP 1 COUNT([preLink].[dbo].[viewDoctorRequests].[Name]) AS [Name] FROM (( [preLink].[dbo].[Request] INNER JOIN [preLink].[dbo].[Request_DoctorLocation] ON [preLink].[dbo].[Request].[RequestId]=[preLink].[dbo].[Request_DoctorLocation].[RequestId]) INNER JOIN [preLink].[dbo].[DoctorLocation] ON [preLink].[dbo].[DoctorLocation].[DoctorLocationId]=[preLink].[dbo].[Request_DoctorLocation].[DoctorLocationId]) WHERE ( [preLink].[dbo].[viewDoctorRequests].[DateTimeRequested] BETWEEN @DateTimeRequested5 AND @DateTimeRequested6)) > @TotalCount4)))',N'@DateTimeRequested1 datetime,@DateTimeRequested2 datetime,@DoctorInvolvementTypeId3 uniqueidentifier,@DateTimeRequested5 datetime,@DateTimeRequested6 datetime,@TotalCount4 int',@DateTimeRequested1='2004-01-01 00:00:00:000',@DateTimeRequested2='2007-05-11 00:00:00:000',@DoctorInvolvementTypeId3='69274150-6835-468E-8798-84788BB3027C',@DateTimeRequested5='2004-01-01 00:00:00:000',@DateTimeRequested6='2007-05-11 00:00:00:000',@TotalCount4=5
How can I create the correct sql?