Slicing a FormCollection by keys that start with a certain string

前端 未结 2 1623
感情败类
感情败类 2021-01-12 07:42

Is there a nice linqy way of splitting a FormCollection into a Dictionary that contains only those keys that start wi

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-12 08:36

    If you're looking for a "nice" way of taking an existing dictionary, producing a new dictionary with copies of keys+values, for a subset of the keys, some LINQ code will do this nicely:

    var appSettings = formCollection.AllKeys
        .Where(k => k.StartsWith("AppSettings."))
        .ToDictionary(k => k, k => formCollection[k]);
    

提交回复
热议问题