Tuesday, March 13, 2012 2:19 PM
I am trying to add some validation that ensure that the same machine cannot have two allocation periods which overlap, so far I have the following:
Private Sub Allocation_Validate(entity As AllocationItem, results As EntitySetValidationResultsBuilder) If Me.Allocation IsNot Nothing Then Dim OverlappingAllocations = From a In Me.Allocation Where entity.Id <> a.Id _ AndAlso entity.MachineItem.Id = a.MachineItem.Id _ AndAlso entity.StartDate >= a.StartDate _ AndAlso (entity.StartDate <= a.EndDate Or a.EndDate Is Nothing) If OverlappingAllocations.any Then results.AddEntityError("Overlapping Allocations") End If End If End Sub
Unfortunately the 'OverlappingAllocation.any' fails as is it not a member. How do I correctly write this?
Thanks in advance for any help.
Tuesday, March 13, 2012 2:23 PM
i am not in front of LS now, but You can try .Count instead of .Any (.Count >0 or .Count >1, check what does better job for You.)
hope this helps.
Tuesday, March 13, 2012 2:27 PM
What is the type of Me.Allocation? Is it an EntityCollection of some type, is it a Query, or is it something else?
Tuesday, March 13, 2012 2:32 PM
also - if this is SERVER tier Validation, i believe You should change Me.Allocation to entity.Allocation,
maybe i am not right, but great if you could check on that.
Tuesday, March 13, 2012 2:38 PM
Thanks for the prompt responses.
@Miroslav - when trying .count I get '.count is not a member of 'Microsoft.Lightswitch.IDataServiceQueryable(Of LightswitchApplication.AllocationItem''
@snomis - does the above information help? I believe its an entity collection.
@Miroslav - yes I believe it is server ties as it in entity validation rather than property validation. Changing me.allocation to entity.allocation fails as .allocation is not a field within entity.
Tuesday, March 13, 2012 2:52 PM
it should work, both for Count and Any, but i am pretty much You should use entity.??? rather than Me.??? in entity validation.
OK, "Allocation" is table name, and in Your business logic You should work with table fields that entity. is having.
i dont know if this helps it all.... :) but what you are trying to achieve is nearly working, so i would suggest You try using only entity. and bypass Me. for this function.
Tuesday, March 13, 2012 2:57 PM
It doesn't seem like Me.Allocation is an entity collection. I say that because it appears that that your validate method is the EntitySet validator on the service tier as Miroslav indicated earlier. That means Me is the DataService which cannot contain entity collection members unless you added one in your parital class. The behavior you are getting seems to indicate Me.Allocation is a query or EntitySet. If this is really what you want, you will not be able to use an Any operator as it is not supported in LightSwitch queries. Instead you can use the FirstOrDefault operator and check for null.
If OverlappingAllocations.FirstOrDefault() is Not Nothing Then
- Proposed As Answer by Miroslav Marinković Tuesday, March 13, 2012 3:01 PM
Tuesday, March 13, 2012 3:00 PM
is ALLOCATION table "child" table? of lets say MACHINE table?
then instead of Me.Allocation, You should try with
entity.MACHINE.Allocation and it should work...
i hope i am not giving YOu headache! :-)
- Marked As Answer by Graham Clements Tuesday, March 13, 2012 3:36 PM
Tuesday, March 13, 2012 3:36 PM
On the contrary Miroslav, not solving this is giving me a headache, but I believe you have relieved my headache.
That is now working, many thanks!
Private Sub Allocation_Validate(entity As AllocationItem, results As EntitySetValidationResultsBuilder) If Me.Allocation IsNot Nothing Then Dim OverlappingAllocations = From a In entity.MachineItem.Allocation Where a IsNot entity _ AndAlso (entity.StartDate >= a.StartDate And (entity.StartDate <= a.EndDate Or a.EndDate Is Nothing)) If OverlappingAllocations.Any Then results.AddEntityError("The allocation start and end date overlaps another allocation start and end date for the same item") End If End If End Sub