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

2021/4/28 12:26:41

本文主要是介绍List根据指定字段进行分组C#,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

  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);
      }

 



这篇关于List根据指定字段进行分组C#的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程