Click here to Skip to main content
15,504,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I have 2 simple LINQ codes written in VB NET below. Which are to select record from database table.
From the below 2 statements, they are almost identical, just the second query has one more condition in WHERE clause.
VB
Dim queryOne = (From queryOneIn DatabaseContext.TableA
				Where queryOne.SC = 123).ToList()(0)
queryOne.RQ = 100
DatabaseContext.AddObject(queryOne)

Dim queryTwo = (From queryTwo In DatabaseContext.TableA
				Where queryTwo.SC = 123 AndAlso queryTwo.RQ = 200).ToList()(0)
DatabaseContext.RemoveObject(queryTwo)

DatabaseContext.SaveChanges()


I was trying to get identical records from queryOne and queryTwo respectively, this is due to I want to insert a new record(the one "queryOne.RQ = 100") and then delete the old record(from queryTwo).
But when I debug the codes, I found that whenever the "queryOne.RQ = 100" is executed, the record fetched from "queryTwo" would be updated to 100 as well for its RQ column, thus resulting in I cannot delete the correct old record, which the RQ suppose to be 200. I was expecting a new line of record is inserted whenever AddObject() is invoked and not update directly.

I am not sure about this LINQ behavior whereas I was querying from 2 different codes by fetching the records into queryOne and queryTwo respectively, I was expecting the queryTwo would contains old record. But I am not sure why.

Any help would be appreciated.
Thank you.

What I have tried:

1. Search online for explanation but clueless on the keyword that I should use.
Posted
Updated 29-Aug-22 21:37pm
v2
Comments
Gerry Schmitz 30-Aug-22 12:20pm    
You're playing with uncommitted data on the client side. We can't tell what the keys are, if any. You're running queries that can return multiple results which you treat as if they were singles. It's basically a poorly designed solution to an ill-defined problem.
Member 15627495 4-Sep-22 3:51am    
Hello,
https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/andalso-operator
the andalso work as evaluating the first 'but maybe not the second parameters...
just use 'and' in your second query

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900