C# 根据Asp.net c中另一个数据表中存在的值更新数据表字段#
嗨,我有两个数据表 桌子 艾斯法夫 1 0 2 0 3 0 4 0 5 0 易变的 身份证件 2. 3. 因此,如果ID存在于C# 根据Asp.net c中另一个数据表中存在的值更新数据表字段#,c#,asp.net,.net,linq,linq-to-dataset,C#,Asp.net,.net,Linq,Linq To Dataset,嗨,我有两个数据表 桌子 艾斯法夫 1 0 2 0 3 0 4 0 5 0 易变的 身份证件 2. 3. 因此,如果ID存在于FavTable中,我想将table1字段isFav更新为1 有谁能在SQL中帮助我解决这个问题吗 UPDATE table SET isfav = 1 WHERE EXISTS(SELECT * FROM favtable WHERE favtable.id = table.id) 在Linq到SQL中 //Create DataContext f
FavTable
中,我想将table1
字段isFav
更新为1
有谁能在SQL中帮助我解决这个问题吗
UPDATE table SET isfav = 1 WHERE EXISTS(SELECT * FROM favtable WHERE favtable.id = table.id)
在Linq到SQL中
//Create DataContext first
foreach (var rec in from a in dc.table
join b in dc.favtable on a.id equals b.id
select a)
{
a.isfav = true;
}
dc.SubmitChanges();
…或(更高效)
由于在注释中提到它是
DataTable
,因此您可以在这些表之间使用Join
,如下所示,并更新字段
table1.AsEnumerable()
.Join( table2.AsEnumerable(),
t1 => t1.Field<int>("id"),
t2 => t2.Field<int>("id"),
(t1, t2) => new { t1 })
.ToList()
.ForEach(o => o.t1["isfav"] = 1);
表1.AsEnumerable()
.Join(表2.AsEnumerable(),
t1=>t1.字段(“id”),
t2=>t2.字段(“id”),
(t1,t2)=>新的{t1})
托利斯先生()
.ForEach(o=>o.t1[“isfav”]=1);
检查此项首先编写一些代码这是
数据库
表还是c#数据表
?
//Create DataContext first
foreach (var rec in from a in dc.table
join b in dc.favtable on a.id equals b.id
select a)
{
a.isfav = true;
}
dc.SubmitChanges();
dc.ExecuteCommand("UPDATE table SET isfav = 1 WHERE EXISTS(SELECT * FROM favtable WHERE favtable.id = table.id)");
table1.AsEnumerable()
.Join( table2.AsEnumerable(),
t1 => t1.Field<int>("id"),
t2 => t2.Field<int>("id"),
(t1, t2) => new { t1 })
.ToList()
.ForEach(o => o.t1["isfav"] = 1);