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

时间:2021-04-28 12:20:20   收藏:0   阅读:0
  var sql = @"update [TABLE] WITH (ROWLOCK)
                                                                          SET 
                                                                         匹配方式 = ‘{0}‘  
                                                                         where ID in({1})";
//lissqls 是你要分组的泛型集合
  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);
      }

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!