简体   繁体   中英

How to Update Column based on another table column value

I have two data tables

table

id  isfav
---------
1   1
2   1
3   1
4   1
5   1

favtable id 
-----------    
  2    true
  3    false

So I want to update the table1 column isFav to 0 if the ids exist in FavTable with false.

Can anybody help me with this?

You can use Any() to search in other entities.

var db = new YourDbContext();
var favtable = db.favtable.ToList();

//Find them:
var result = db.Table1.Where(t => favtable.Any(f => f.id == t.id && !f.isfav));

//result should be 3.

.NET Fiddle: https://dotnetfiddle.net/BmaqN5

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