public async Task<bool> UpdateStatus(int[] ids) { var blogs = _efCoreContext.Blogs .Where(d => ids.Contains(d.Id)); var updateProductList = blogs.Select(b => new Blog() { Id = b.Id, Status = 1 }).ToList(); _efCoreContext.UpdateRange(updateProductList, true, d => d.Status); if (await _efCoreContext.SaveChangesAsync(CancellationToken.None) > 0) { return true; } return false; }
´ËʱÓëÊý¾Ý¿â»á½øÐÐÁ½´ÎÁ¬½Ó£¬Ò»´ÎÊDzéѯ£¬Ò»´ÎÊǸüÐÂÖ¸¶¨ÊôÐÔ×ֶΣ¬Í¨¹ýSQL¸ú×ÙÎÒÃÇÄÜ¿´µ½ÈçÏÂÓï¾ä£º
SELECT [d].[Id] FROM [Blog] AS [d] WHERE [d].[Id] IN (2, 3, 5, 6)
exec sp_executesql N'SET NOCOUNT ON; UPDATE [Blog] SET [Status] = @p0 WHERE [Id] = @p1; SELECT @@ROWCOUNT; UPDATE [Blog] SET [Status] = @p2 WHERE [Id] = @p3; SELECT @@ROWCOUNT; UPDATE [Blog] SET [Status] = @p4 WHERE [Id] = @p5; SELECT @@ROWCOUNT; UPDATE [Blog] SET [Status] = @p6 WHERE [Id] = @p7; SELECT @@ROWCOUNT; @p1 int,@p0 tinyint,@p3 int,@p2 tinyint,@p5 int,@p4 tinyint,@p7 int,@p6 tinyint',@p1=2,@p0=1,@p3=3,@p2=1,@p5=5,@p4=1,@p7=6,@p6=1
×îÖÕÕýÈ·¸üÐÂÈçÏ£º
³ýÁËÉÏÊöͨ¹ýд·´ÉäÀ©Õ¹·½·¨À´¸üÐÂÍâÊôÐÔÍ⣬һֱÔÚÏë×ÅÆäÖлá½øÐÐÁ½´ÎÊý¾Ý¿âÁ´½Ó£¬½øÐÐÒ»´ÎÊý¾Ý¿âÁ´½Ó±È½ÏºÄʱ£¬Õâ¸öʱºòÏëµ½µÄÖ»ÄÜÖ´ÐÐSQLÃüÁîÁË¡£
ÅúÁ¿¸üÐÂÖ®SQLÃüÁîÀûÓÃWHERE ....INÀ´½øÐиüУ¬´ËʱSQL¸üдúÂëÔòÈçÏ£º
public async Task<bool> UpdateStatus(int[] ids) { , ids); , CancellationToken.None, testIds); if (effctRow > 0) { return true; } return false; }
²»ÖªµÀ¸÷λ¿´¿Í·¢ÏÖʲôûÓУ¬ÉÏÊöµÄ´úÂëÊÇÓÐÎÊÌâµÄ£¬ÄÄÀïÓÐÎÊÌ⣬²»ÖªµÀµÄÇë¿´Èç϶¯Ì¬ÑÝʾ¡£
Õý³£Çé¿öÏÂWHERE...IN(2,3,5,6)¶ø·ÇÉÏÊö“2,3,5,6”£¬´ËʱÎÒ½«ÉÏÊö´úÂëÐÞ¸ÄΪÈçÏ£º
public async Task<bool> UpdateStatus(int[] ids) { var blogIds = string.Empty; foreach (var id in ids) { blogIds += $; } blogIds = blogIds.TrimEnd(); , CancellationToken.None, blogIds); if (effectRows > 0) { return true; } return false; }
´ËʱÔÙÀ´¿´¿´ÑÝʾЧ¹û£º
´ËʱÔò±¨NVARCHARת»»µ½INTʧ°Ü£¬ÄÇô´Ö±©Ò»µã½«idת»»ÎªNVARCHAR:
, CancellationToken.None, blogIds);
µ±È»ÈçÉÏÉæ¼°µ½Ë÷Òý£¬Í¨¹ýº¯Êýת»»²»»á×ßË÷Òý£¬ÎÒÃÇÕý³£Çé¿öÏÂÓ¦¸ÃÊǶ¨ÒåÒ»¸ö±äÁ¿½«id½øÐÐת»»£¬È»ºóÀûÓñäÁ¿À´½øÐаüº¬¡£´ËʱÔÙÀ´¿´ÑÝʾЧ¹û£º
´Ëʱѹ¸ù¶¼Ã»È¥¸üУ¬ÎÒÒ²ÊÇ×íÁË£¬×îºóÎÒÃÇÔÙÀ´¿´Ò»ÖÖÇé¿ö£¬ÎÒÃÇдSQLÃüÁîͨ¹ýÆ´½ÓµÄÐÎʽÀ´½øÐУ¬ÈçÏ£º
, CancellationToken.None);
´Ëʱ¾ÓÈ»¸üгɹ¦ÁË£¬ÆäʵÎÒÃÇÀûÓÃÉÏÊö×Ö·û´®Æ´½ÓµÄ·½Ê½½øÐÐÈçÏÂÁ½ÖÖת»»¶¼»á¸üгɹ¦£º
¡¡