简体   繁体   中英

C# LINQ join With Just One Row

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM