Vibrant discussion about CSLA .NET and using the framework to build great business applications.

Forum has moved

New location: CSLA .NET forum

CSLA .NET Resources:
  • CSLA .NET forum
  • CSLA .NET home page
  • Linq query returning incorrect results when object is not saved..

    rated by 0 users
    Answered (Not Verified) This post has 0 verified answers | 5 Replies | 1 Follower

    Top 50 Contributor
    179 Posts
    Vinodonly posted on Thu, Feb 25 2010 4:17 AM

    I'm calling following code from my validation method.. This seems to work when the object is saved.. if i delete a item and add it again (without saving) then this is returning incorrect results.. whereas in debugger i can see that item is sucessfully deleted and added but linq query is not returning correct result.. It seems like a bug...

    DateTime MaxDepDate = (from J in target.JoinInfoList

    where J.JoinFlag == DepDtId

    orderby J.InfoDate.XToDateTime() descending

    select J.InfoDate.XToDateTime()).FirstOrDefault();

    All Replies

    Top 500 Contributor
    17 Posts
    Answered (Not Verified) bartol replied on Thu, Feb 25 2010 6:16 AM
    Suggested by Blake Niemyjski

    Are you sure that DeptId is not zero before the object is saved? If that property is a foreign key that is only set after the insert then it would explain the problem.

    Top 50 Contributor
    179 Posts

    depdtid is a fix field which i'm retrieving from my global control filler.. it is not zero but a valid value which points to a id.. as long as i can see in my tests, it seems like a bug..

    Top 10 Contributor
    9,475 Posts

    JoinInfoList is a BLB?

    And you are removing an item, then adding another item that has the same DepDtd value?

    What do you think should happen in that case? I would expect that the query should work against this newly added item.

    When you delete the item, it is moved to DeletedList. When you add another item with the same DepDtd value it is added to the active list. When you save the list, DeletedList items are deleted first, then active items are inserted/updated.

    But the active list is always can always be queried, and whatever is in the active list should be part of the results.


    Top 50 Contributor
    179 Posts

    i think maybe i'm not understanding it correctly.. this is the replacement code which i'm using which solves the issue.. you are writing that linq should return the result from current items but that is not happenning, it is returning from the last saved items.. Maybe i'm not understanding your post correctly..

    Here is the object graph EmpMain is the parent which is having Child list as JoinInfoList with JoinInfo child objects.. The below validation is called from parent i.e. EmpMain..








    DateTime MaxJoinDate = DateTime.MinValue;



    DateTime MaxDepDate = DateTime.MinValue;



    DateTime TmpDt = DateTime.MinValue;



    foreach (var item in  target.JoinInfoList)


    TmpDt = item.InfoDate.XToDateTime();




    if  (item.JoinFlag == JoinDtId && TmpDt > MaxJoinDate)

    MaxJoinDate = TmpDt;



    if  (item.JoinFlag == DepDtId && TmpDt > MaxDepDate)



    Top 10 Contributor
    9,475 Posts

    LINQ should just do its query over the items that are active in the BLB. In fact, LINQ queries over objects typically translate into foreach statements. So I don't know why there would be different behavior - but obviously you have a workaround, so that's probably what counts most.


    Page 1 of 1 (6 items) | RSS

    Copyright (c) 2006-2014 Marimer LLC. All rights reserved.
    Email admin@lhotka.net for support.
    Powered by Community Server (Non-Commercial Edition), by Telligent Systems