根据值更新多行,使用 C# DBContext 和本机查询
Updating Multiple Rows, Depending on Value, Using C# DBContext, and native Query
public class Selecter
{
public string SelecterId { get; set; }
public string UniqueText { get; set; }
public string OutputError { get; set; }
public bool Selected { get; set; }
}
modelBuilder.Entity("WebAssistantClient.Models.Selecter", b =>
{
b.Property<string>("SelecterId").HasColumnType("nvarchar(450)");
b.Property<string>("OutputError").HasColumnType("nvarchar(max)");
b.Property<bool>("Selected").HasColumnType("bit");
b.Property<string>("UniqueText").HasColumnType("nvarchar(max)");
b.HasKey("TestSelecterId");
b.ToTable("TestSelecters");
}
);
CREATE TABLE [dbo].[Selecters](
[SelecterId] [nvarchar](450) NOT NULL,
[UniqueText] [nvarchar](max) NULL,
[OutputError] [nvarchar](max) NULL,
[Selected] [bit](max) NULL,
CONSTRAINT [PK_Selecters] PRIMARY KEY CLUSTERED ([SelecterId] ASC)
)
假设这是我的初始数据:
SelecterId UniqueText Selected
"fruit0" "apple" false
"fruit1" "banana" true
"fruit2" "lemon" false
"fruit3" "" false
要求 1
当我将某些行的 Selected 列设置为 true,但将其他行的 Selected 列更改为 false 时,我需要:
示例:对于“fruit0”选择为 true,其列中的其他行 Selected 将为 false,在本例中为“fruit1”
SelecterId UniqueText Selected
"fruit0" "apple" true
"fruit1" "banana" false
"fruit2" "lemon" false
"fruit3" "" false
要求 2
我需要将 UniqueText 列设置为某行的值,但如果包含相同的值,则将另一行 UniqueText 列更改为“”(空):
示例:对于“fruit3”UniqueText 为“lemon”,具有相同值的另一行变为空“”,在本例中为“fruit2”
SelecterId UniqueText Selected
"fruit0" "apple" true
"fruit1" "banana" false
"fruit2" "" false
"fruit3" "lemon" false
使用SQL服务器,是否可以一句话完成要求?
使用 C#,如何使用 context.Selecter.FromSqlRaw("???");
Using SQL Server, Is it possible to do the requirements in one single sentence?
您 select both/all 您感兴趣的,然后使用条件集来计算要应用的值
Example: for "fruit0" Selected to true, the other rows in their colum Selected will be false, in this case the "fruit1"
UPDATE fruits
SET Selected = CASE WHEN SelecterId = 'fruit0' THEN 'true' ELSE 'false' END
WHERE selecterId = 'fruit0' OR Selected = 'true'
for "fruit3" UniqueText to "lemon", the another row with same value become to empty "", in this case the "fruit2"
UPDATE fruits
SET UniqueText = CASE WHEN SelecterId = 'fruit3' THEN 'lemon' ELSE '' END
WHERE SelecterId = 'fruit3' OR UniqeText = 'lemon'
作为一般模式:
UPDATE table
SET column_to_change = CASE WHEN identifying_column = 'id_value_you_know' ELSE 'resting_value' END
WHERE identifying_column = 'id_value_you_know' OR column_to_change = 'resting_value'
您应该始终知道您更改的列、您标识某些记录的列、您标识的值以及您更改的列的剩余值
public class Selecter
{
public string SelecterId { get; set; }
public string UniqueText { get; set; }
public string OutputError { get; set; }
public bool Selected { get; set; }
}
modelBuilder.Entity("WebAssistantClient.Models.Selecter", b =>
{
b.Property<string>("SelecterId").HasColumnType("nvarchar(450)");
b.Property<string>("OutputError").HasColumnType("nvarchar(max)");
b.Property<bool>("Selected").HasColumnType("bit");
b.Property<string>("UniqueText").HasColumnType("nvarchar(max)");
b.HasKey("TestSelecterId");
b.ToTable("TestSelecters");
}
);
CREATE TABLE [dbo].[Selecters](
[SelecterId] [nvarchar](450) NOT NULL,
[UniqueText] [nvarchar](max) NULL,
[OutputError] [nvarchar](max) NULL,
[Selected] [bit](max) NULL,
CONSTRAINT [PK_Selecters] PRIMARY KEY CLUSTERED ([SelecterId] ASC)
)
假设这是我的初始数据:
SelecterId UniqueText Selected
"fruit0" "apple" false
"fruit1" "banana" true
"fruit2" "lemon" false
"fruit3" "" false
要求 1
当我将某些行的 Selected 列设置为 true,但将其他行的 Selected 列更改为 false 时,我需要: 示例:对于“fruit0”选择为 true,其列中的其他行 Selected 将为 false,在本例中为“fruit1”
SelecterId UniqueText Selected
"fruit0" "apple" true
"fruit1" "banana" false
"fruit2" "lemon" false
"fruit3" "" false
要求 2
我需要将 UniqueText 列设置为某行的值,但如果包含相同的值,则将另一行 UniqueText 列更改为“”(空): 示例:对于“fruit3”UniqueText 为“lemon”,具有相同值的另一行变为空“”,在本例中为“fruit2”
SelecterId UniqueText Selected
"fruit0" "apple" true
"fruit1" "banana" false
"fruit2" "" false
"fruit3" "lemon" false
使用SQL服务器,是否可以一句话完成要求?
使用 C#,如何使用 context.Selecter.FromSqlRaw("???");
Using SQL Server, Is it possible to do the requirements in one single sentence?
您 select both/all 您感兴趣的,然后使用条件集来计算要应用的值
Example: for "fruit0" Selected to true, the other rows in their colum Selected will be false, in this case the "fruit1"
UPDATE fruits
SET Selected = CASE WHEN SelecterId = 'fruit0' THEN 'true' ELSE 'false' END
WHERE selecterId = 'fruit0' OR Selected = 'true'
for "fruit3" UniqueText to "lemon", the another row with same value become to empty "", in this case the "fruit2"
UPDATE fruits
SET UniqueText = CASE WHEN SelecterId = 'fruit3' THEN 'lemon' ELSE '' END
WHERE SelecterId = 'fruit3' OR UniqeText = 'lemon'
作为一般模式:
UPDATE table
SET column_to_change = CASE WHEN identifying_column = 'id_value_you_know' ELSE 'resting_value' END
WHERE identifying_column = 'id_value_you_know' OR column_to_change = 'resting_value'
您应该始终知道您更改的列、您标识某些记录的列、您标识的值以及您更改的列的剩余值