List根据指定字段进行分组C#

  var sql = @"update [TABLE] WITH (ROWLOCK)
                                                                          SET 
                                                                         匹配方式 = '{0}'  
                                                                         where ID in({1})";
//lissqls 是你要分组的泛型集合
   List<UpdateSqlHelper>lissqls=new List<UpdateSqlHelper>();
   var GroupList = lissqls.GroupBy(x => x.MatchType).Select(x => new{ MatchType = x.Key, ItemIDLis = x.ToList() });
//存储更新语句
   List<string> update_bill_sql = new List<string>();

     foreach (var item in GroupList)
      {
         List<int> itemids = new List<int>();
          foreach (var itemID in item.ItemIDLis)
           {
            itemids.Add(itemID.ItemID);
           }
           var newsql = string.Format(sql, item.MatchType, string.Join(",", Array.ConvertAll<int, string>(itemids.ToArray(), delegate (int x) { return x.ToString(); })));
           update_bill_sql.Add(newsql);
      }

 

  public class UpdateSqlHelper
    {
        public string MatchType { get; set; }
        public int ItemID { get; set; }
        public string Guid { get; set; }
    }

 

posted @ 2021-04-28 10:08  是晚安呀  阅读(1603)  评论(0编辑  收藏  举报