tags:

views:

57

answers:

1

I'm trying to make a query that grabs a single row from an SQL database and updates it.

TableA
AId
AValue

TableB
BId
AId
BValue

Ok, so TableA and TableB are linked by AId. I want to select a row in TableB based on AValue using a join. The following query is what I have and only grabs a value from TableB based on AId, I just don't know how to grab a row from TableB using AValue. I know you would need to use a join, but I'm not sure how to accomplish that.

var row = DbObject.TableB.Single(x => x.AId == 1)
row.BValue = 1;
DbObject.SubmitChanges();
A: 

Below is a LINQ query to do what you are asking.

var row = (from b in TableB
        join a in TableA on a.AId equals b.AId
        where a.AValue = yourValue).Single();
Taylor Leese
Ok, from there, do I have to grab the result using a foreach loop?There is only going to be one value grabbed in the query. Is there a more efficient way to get the result because there is only one expected value?
Soo
I edited my answer to use Single() so you don't have to use a foreach loop.
Taylor Leese
a.AId equals b.AId^ a quick correction, otherwise, it seems to be working so heyo! :Dthanks
Soo
Looks like someone already fixed up my answer.
Taylor Leese