Inconsistent results from FindMatches

Posts   
 
    
Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 20-Nov-2007 01:18:38   

Hi, (Using Ver 2/Adapter/VB/SQL 2K/WinForms)

When using FindMatches in two slightly different scenarios, I get inconsistent results. In both of the following cases, I'm checking for a duplicate entry in a grid.

  1. In the code below, the entry is a duplicate if matches.Count > 1
        'Check for duplicate ID in grid collection
        Dim myView As GridView = GridView1
        Dim ID As Integer = CInt(myView.GetFocusedRowCellValue("RoleID"))
        Dim curCollection As EntityCollection(Of RoleEntity)
        curCollection = CType(Me.BindingSource1.DataSource, EntityCollection(Of RoleEntity))
        Dim filter As New PredicateExpression()
        filter.Add(RoleFields.RoleID = ID)
        Dim matches As List(Of Integer) = curCollection.FindMatches(filter)
        If matches.Count > 1 Then
          'Its an error

  1. In this code, the entry is a duplicate only if matches.Count > 0
        'Check for duplicate ID in grid collection
        Dim myView As GridView = GridView1
        Dim div As String = Me.cboDivCode.Text.ToString
        Dim ID As String = CStr(myView.GetFocusedRowCellValue("ProgrammeCode"))
        Dim curCollection As EntityCollection(Of BudgetDivisionEntity) = allBudgetDivisions
        Dim filter As New PredicateExpression()
        filter.Add(BudgetDivisionFields.DivisionCode = div).AddWithAnd(BudgetDivisionFields.ProgrammeCode = ID)
        Dim matches As List(Of Integer) = curCollection.FindMatches(filter)
        If matches.Count > 0 Then
          'Its an error

The only significant difference is in 2, where the grid's datasource is a filtered EntityView2 on allBudgetDivisions. So curCollection is sourced from allBudgetDivisions which is a class variable defined as:

    Private allBudgetDivisions As EntityCollection(Of BudgetDivisionEntity)

In 1 above, matches.Count includes the newly entered cell value + any matches. In 2 however, matches.Count is a count of any matches, only.

I can't see why there should be a difference. Any ideas confused

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 20-Nov-2007 03:59:22   

Hi Markiemac,

I'm trying to repro that, I'm not sure what the scenario is. Could you do a simpler snippet test and give us the steps to reproduce that behavior?

Cheers

David Elizondo | LLBLGen Support Team
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39908
Joined: 17-Aug-2003
# Posted on: 20-Nov-2007 10:59:09   

If you perform a FindMatches on the collection which you filtered, you will take into account the entities which are perhaps NOT in the view. So if you want to find a match, create a new collection from the view, then execute find matches on that one.

You can also loop through the view which is faster (as you then loop just once through the view).

Btw, I have a hard time understanding what exactly you did expect and what the code did instead.

Frans Bouma | Lead developer LLBLGen Pro
Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 21-Nov-2007 00:59:14   

Guys, Seems I was both obscure and verbose and on consideration I'm taking up Frans' suggestions.

So if you want to find a match, create a new collection from the view, then ...

I know this is basic stuff flushed , but just how do I create a new collection from a view.

You can also loop through the view which is faster

I have a function that returns True if an entity is in a collection.

Public Shared Function EntityInCollection(ByVal collection As IEntityCollection2, ByVal fieldName As String, ByVal key As String) As Boolean

.. but when I try to pass the following

If Utilities.EntityInCollection(CType(BudgetDivisionBindingSource.DataSource, IEntityCollection2), "ProgrammeCode", ID) = True Then

I get this casting error:

Unable to cast object of type 'SD.LLBLGen.Pro.ORMSupportClasses.EntityView2`1[MIS.DAL.EntityClasses.BudgetDivisionEntity]' to type 'SD.LLBLGen.Pro.ORMSupportClasses.IEntityCollection2'.

Note: BudgetDivisionBindingSource.DataSource was populated from a filtered EntityView2.

Not sure how to resolve this. Thanks

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 21-Nov-2007 09:24:45   

I know this is basic stuff , but just how do I create a new collection from a view.

entityView.ToEntityCollection();

I have a function that returns True if an entity is in a collection. Code: Public Shared Function EntityInCollection(ByVal collection As IEntityCollection2, ByVal fieldName As String, ByVal key As String) As Boolean

.. but when I try to pass the following Code: If Utilities.EntityInCollection(CType(BudgetDivisionBindingSource.DataSource, IEntityCollection2), "ProgrammeCode", ID) = True Then

I get this casting error:Quote: Unable to cast object of type 'SD.LLBLGen.Pro.ORMSupportClasses.EntityView2`1[MIS.DAL.EntityClasses.BudgetDivisionEntity]' to type 'SD.LLBLGen.Pro.ORMSupportClasses.IEntityCollection2'.

Note: BudgetDivisionBindingSource.DataSource was populated from a filtered EntityView2.

The exception explains itself simple_smile as indeed you cant cast an EntityView to an IEntityCollection, instead use the ToEntityCollection() method to get an EntityCollection from the entityView.

Anyway I guess the issue here might be when these couple of code blocks are called. Are you sure that when you execute the 2nd code block that the new entity is already inserted in the EntityCollection where you want to find the matches, like the case in the first code block. You can debug this by checking the Count property of the EntityCollection.

Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 21-Nov-2007 11:14:17   

Walaa,

.. instead use the ToEntityCollection() method to get an EntityCollection from the entityView.

I only found the ToEntityCollection() method after I'd posted, which resolved my casting error.

Anyway I guess the issue here might be when these couple of code blocks are called. Are you sure that when you execute the 2nd code block that the new entity is already inserted in the EntityCollection where you want to find the matches, like the case in the first code block. You can debug this by checking the Count property of the EntityCollection.

Spot on! simple_smile

Thanks for your patience.