从C#中缺少键的列表中创建完整的集合



我做了很多挖掘,似乎找不到这个特定问题的答案;类似问题的答案,但没有完全像这样。

从本质上讲,我试图做的是在列表中添加具有默认值的缺失键。我有一个列表<KeyValuePair<字符串,字符串>gt;((具有以下结构:

关键字:值

name : Orange
actualname : Orango
name : Lime
fullname : Lime Lime
actualname : Limo

从上面的列表中,我想创建一个完整的集合,该集合中缺少键。

预期键:值

name : Orange
fullname : ""
actualname : Orango
name : Lime
fullname : Lime Lime
actualname : Limo

我正在尝试以下代码:

var list = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("name", "Orange"),
new KeyValuePair<string, string>("actualname", "Orango"),
new KeyValuePair<string, string>("name", "Lime"),
new KeyValuePair<string, string>("fullname", "Lime Lime"),
new KeyValuePair<string, string>("actualname", "Limo")
};
var distinctKeys = list
.Select(pair => pair.Key)
.Distinct()
.OrderBy(pair => pair)
.ToArray();
var lastKeyIndex = -1;
for (var index = 0; index < list.Count; index++)
{
var currentKeyIndex = lastKeyIndex + 1 == distinctKeys.Length ? 0 : lastKeyIndex + 1;
var currentKey = distinctKeys[currentKeyIndex];
if (list[index].Key != currentKey)
{
list.Insert(index, new KeyValuePair<string, string>(currentKey, string.Empty));
}
lastKeyIndex = currentKeyIndex;
}
for (var index = lastKeyIndex+1; index < distinctKeys.Length; index++)
{
list.Add(new KeyValuePair<string, string>(distinctKeys[index], string.Empty));
}

但它并没有给我带来预期的产出。

另一套尝试:

关键字:值

contacts.coid : 2003984
createdon : 2020-09-10
c_id : fcd5937d
contacts.coid : 2024489
createdon : 2020-09-10
contacts.fullname : Mark
contacts.coid : 99
c_id : 7e70096e
contacts.coid : 2024496
createdon : 2020-09-10
contacts.fullname : Simon
c_id : ebbbd1f4

预期输出

预期键:值

contacts.coid : 2003984
createdon : 2020-09-10
contacts.fullname : ""
c_id : fcd5937d
contacts.coid : 2024489 
createdon : 2020-09-10
contacts.fullname : Mark
c_id : ""
contacts.coid : 99
createdon : ""
contacts.fullname : ""
c_id : 7e70096e
contacts.coid : 2024496
createdon : 2020-09-10
contacts.fullname : Simon
c_id : ebbbd1f4

任何解决这一问题的想法都将受到欢迎。

给定每个分组的第一个键,您可以在此基础上进行分组,您可以从每个集合中创建一个按部分排序的键的完整列表,然后扩展每个组以获得一个完整的键集。

首先,IEnumerable上的一些扩展允许您在谓词上分组(当每个组是true时开始(,DistinctBy的扩展:

public static class IEnumerableExt {
// TRes seedFn(T FirstValue)
// TRes combineFn(TRes PrevResult, T CurValue)
// Based on APL scan operator
// Returns TRes
public static IEnumerable<TRes> Scan<T, TRes>(this IEnumerable<T> items, Func<T, TRes> seedFn, Func<TRes, T, TRes> combineFn) {
using (var itemsEnum = items.GetEnumerator()) {
if (itemsEnum.MoveNext()) {
var prev = seedFn(itemsEnum.Current);
while (itemsEnum.MoveNext()) {
yield return prev;
prev = combineFn(prev, itemsEnum.Current);
}
yield return prev;
}
}
}
// returns groups of T items each starting when testFn is true
public static IEnumerable<IEnumerable<T>> GroupByUntil<T>(this IEnumerable<T> items, Func<T, bool> testFn) =>
items.Scan(item => (groupNum: 0, theItem: item), (a, item) => testFn(item) ? (a.Item1+1, item) : (a.Item1, item))
.GroupBy(t => t.groupNum)
.Select(tg => tg.Select(t => t.theItem));
// returns a single item from each group of items by keyFn(item) picked by pickFn(itemGroup)
public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> items, Func<T, TKey> keyFn, Func<IGrouping<TKey, T>, T> pickFn, IEqualityComparer<TKey> comparer = null) =>
items.GroupBy(keyFn, comparer).Select(pickFn);
}

给定每组的第一个密钥:

var firstKey = "name";

现在,您可以根据密钥在每组中的出现位置创建密钥的部分排序,然后对不同的密钥进行排序:

var ordering = list.GroupByUntil(kvp => kvp.Key == firstKey)
.OrderBy(g => g.Count())
.SelectMany((g,sn) => g.Select((g, n) => new { g.Key, n = (sn+1)*n }))
.OrderBy(kn => kn.n)
.DistinctBy(kn => kn.Key, g => g.Last())
.ToDictionary(kn => kn.Key, kn => kn.n);
var keySet = list.Select(kvp => kvp.Key).Distinct().OrderBy(k => ordering[k]).ToList();

使用keySet,您可以扩展每组项目以拥有所有密钥:

var ans = list.GroupByUntil(kvp => kvp.Key == firstKey)
.Select(g => g.ToDictionary(l => l.Key, l => l.Value))
.SelectMany(d => keySet.Select(k => new KeyValuePair<string, string>(k, d.TryGetValue(k, out var v) ? v : "")));

如果您希望最终集合仍然分组,只需将SelectMany替换为Select即可。

最新更新