none
Incorrect results when a query is repeated RRS feed

  • Question

  • I'm using Entity framework. I have this scenario :

    ParentObject - has a child collection : Child1.

    On the Form I have 1 checkbox and 1 button. I have a report that display the PArentObject and the corresponding list of Child1.

    the Onclick's event  of the button  has this code :

    context.Configuration.LazyLoadingEnabled = False
    Dim plist as IQueryable(Of ParentObject)
    Dim chlist as IQueryable(of Child1)
    plist=context.Set(Of ParentObject)
    chlist=context.Set(of Child1)
    
    plist.ToList()
    chlist=chlist.Where(Function(t1) plist.Any(Function(t2) t2.id=t1.parent))
    If checkbox1.checked then chlist=chlist.Where(Function(t1) t1.vl>0)
    chlist.ToList()
    MyReport.Datasource=plist.ToList

    These are the result when I press the button :

    1) With checkbox checked , Press the button ( first time ) - The report  display all the Parent , each of them with only the childs
    that have vl1>0 ( **Correct** )
    2) With checkbox unchecked , press the button (second time ) - The report  display all the Parent with all the childs ( **Correct** )
    3) With checkbox checked , press the button ( third time ) - The report display all the parent with all the childs  ( **Not correct** )

    I close the form , and re-open it.
     1) With checkbox unchecked , press the button (First time ) - The report display all the Parent with all the childs ( **Correct** )
     2) With checkbox checked , press the button ( Second time ) - The report display all the parent with all the childs  ( **Not correct** )

    Why I get not correct results when repeating the queries like in the examples above ? What should I do in these cases ? Thank you !

    Tuesday, February 10, 2015 9:23 PM

All replies

  • Hello,

    Creating a similar demo test with yours, however, the query result for the chlist would be affected by the checkbbokx:

    Public Class Form1
    
        Dim db As DFDBEntities = New DFDBEntities
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            db.Configuration.LazyLoadingEnabled = False
    
            Dim plist As IQueryable(Of Order)
    
            Dim chlist As IQueryable(Of OrderDetail)
    
            plist = db.Set(Of Order)()
    
            chlist = db.Set(Of OrderDetail)()
    
    
            plist.ToList()
    
            chlist = chlist.Where(Function(t1) plist.Any(Function(t2) t2.OrderID = t1.OrderID))
    
            If CheckBox1.Checked Then chlist = chlist.Where(Function(t1) t1.OrderDetailName = "1")
    
    
            Dim chlistnew = chlist.ToList()
    
            Dim plistnew = plist.ToList
    
        End Sub
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
           
    
        End Sub
    
    End Class
    

    I am not sure how you binding the chdlist collection since you do not provide it, could you please have a try with my way as define two new list object to accept the query result and check the result. Or please share information as:

    What the MyReport object is, is it a DataGridView or other controls used to display data? How do you binding chdlist to your display control? I am wondering if you have binding the display control for chdlist with the local collection in context.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 11, 2015 7:33 AM
    Moderator
  • Thank you !

    But I prefer to understand why my method produce a such result ?

    MyReport is a report to display and print data.

    The controls inside the report are bound to entity on design.

    But I don't think the problem is the report but the chlist.

    In the second button press , the chlist is filled with all, the childs.

    Why in the third button press , when the checkbox is checked , the chlist is not filtered according to chlist.Where(Function(t1) t1.vl>0)

    so has this effect or not ?

    when a collection is filled , how the entity framework works ? Does keep the items or read again the database ?

    Thank you !



    • Edited by dcode25 Wednesday, February 11, 2015 11:09 PM
    Wednesday, February 11, 2015 11:08 PM
  • Hello,

    >> when a collection is filled , how the entity framework works ? Does keep the items or read again the database ?

    Yes, it actually would keep the requested items in the Local properties, that is why I ask if you have binding the display control with the Local property, if you have, when you uncheck the checkbox, it would load all data to the local and even you check the checkbox again, the local property contains all records.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, February 12, 2015 9:24 AM
    Moderator
  • Hello !

    I never used this "Local" property.

    so it's sure that I haven't used explicitly in this project . But I don't know if the report use this property by default when I bind the controls .

    How can I be sure about this ?

    Thank you

    Thursday, February 12, 2015 4:09 PM
  • Hello,

    >>But I don't know if the report use this property by default when I bind the controls.

    Would you please show some code reagrding the data binding between the reprot control and its datasource list?

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, February 13, 2015 2:38 AM
    Moderator
  • Hello !

    There's no special code about the report.

    the only line of code that I run before opening the report is :

    Report.datasource=plist.ToList

    Inside the report , for each elemnt on plist there's a detail showing all the childs in chlist.

    But inside the report , there's no code. Only controls bound to fields.

    Friday, February 13, 2015 4:16 AM
  • Hello,
    What is the report control? If it is a custom one, I suggest you could post a feedback to its author.
    Regards.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 16, 2015 9:59 AM
    Moderator
  • Standart report of visual studio
    Tuesday, February 17, 2015 4:12 AM
  • Hi,

    Visual Studio contains lots of report controls, as DataGridView, ReportViewer, if you use the crystal reports, this is not supported in this forum.

    So could you tell which one you are using exactly?

    Tuesday, February 17, 2015 9:26 AM
  • Reportviewer
    Tuesday, February 17, 2015 6:38 PM
  • Hello,

    For the reportviewer control, I am not sure if it would cache the record, I suggest that you could ask it to on the reportviewer forum and if it does, you could also ask for a way to clear the cached content:

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=vsreportcontrols

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 18, 2015 7:02 AM
    Moderator