Linq query returning incorrect results when object is not saved..

Linq query returning incorrect results when object is not saved..

Old forum URL:

Vinodonly posted on Thursday, February 25, 2010

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();

bartol replied on Thursday, February 25, 2010

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.

Vinodonly replied on Sunday, February 28, 2010

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..

RockfordLhotka replied on Sunday, February 28, 2010

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.

Vinodonly replied on Monday, March 01, 2010

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)



RockfordLhotka replied on Monday, March 01, 2010

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.

Copyright (c) Marimer LLC