C# 映射KeyValuePair';来自不同IEnumerable&x27;s
问题是: 我需要将我的C# 映射KeyValuePair';来自不同IEnumerable&x27;s,c#,algorithm,linq,lambda,C#,Algorithm,Linq,Lambda,问题是: 我需要将我的reducedList中的数据与我的coreList匹配,某种连接,其中reducedList的键(“年份、Id、房子…”)将与我的coreList的键(列表)匹配这两个结果将合并到临时结果列表templast-这将是一列 最终的目标是使用我后来合并的数据填充整个excel列,其中templast中的第一个条目是标题,其余的是数据 源代码被注释掉了,希望能有所帮助 起初,我的例子是可行的,但我需要一种更好的方法,也许更有效,或者一种不同的方法来解决这个问题,我认为我需要一种
reducedList
中的数据与我的coreList
匹配,某种连接,其中reducedList
的键(“年份、Id、房子…”)将与我的coreList
的键(列表)匹配这两个结果将合并到临时结果列表templast
-这将是一列
最终的目标是使用我后来合并的数据填充整个excel列,其中templast
中的第一个条目是标题,其余的是数据
源代码被注释掉了,希望能有所帮助
起初,我的例子是可行的,但我需要一种更好的方法,也许更有效,或者一种不同的方法来解决这个问题,我认为我需要一种更好的映射方法/算法
这就是我的方法:
核心数据(所有内容):
将精简列表(仅少数属性)中的属性名称与核心列表(所有属性)组合/匹配:
List ergebnisListe=newlist()//“daraus ein Array machen
#与缩减列表匹配的区域
foreach(reducedList中的KeyValuePair reducedListItem)
{
列表模板列表=新建列表();
templast.Add(reducedListItem.Value!=null&&!String.IsNullOrEmpty(reducedListItem.Value)?reducedListItem.Value:“!berschrift_FEHLER!”);//首先添加标题
#区域I正在创建核心列表,以获取我的数据集的其余部分(实际上我需要一些映射)
foreach(coreList中的KeyValuePair项)
{
#我想要的列表的区域过滤器
如果(item.Value是IEnumerable&&item.Value.GetType().IsGenericType)//只匹配列表,我希望使用
{
foreach(Dictionary subItemListItem in item.Value as List)//我正在使用“KeyValuePair”检查某种类型的列表
{
if(subItemListItem.ContainsKey(reducedListItem.Key))//检查有效键
{
//正在使用“subItemListItem.Values”或“Keys”进行操作?!
添加(subItemListItem[reducedListItem.Key]!=null?subItemListItem[reducedListItem.Key].ToString():“错误”);
}
}
}
#端区
}
#端区
#区域将结果添加到我的最终列表
//添加一列/记录
ergebnisListe.Add(新的KeyValuePair(reducedListItem.Key,templast.ToArray());//我需要“string[]”第一条记录是标题,其余是它的相关数据
#端区
}
除了核心列表之外,我几乎可以自由更改数据结构和类型
谢谢您的帮助。这里是LINQ版本:
// Getting dictionaries from coreList
var dictsInCoreList =
coreList
.Values
.Where(value => value is IEnumerable && value.GetType().IsGenericType)
.SelectMany(value => value as List<object>)
.Cast<Dictionary<string, object>>()
.ToList();
// Generate the result
ergebnisListe =
reducedList
.Select(reducedListItem =>
{
var tempList =
new List<string>()
{
String.IsNullOrEmpty(reducedListItem.Value)
? "!Überschrift_FEHLER"
: reducedListItem.Value
};
tempList.AddRange(
dictsInCoreList
.Where(dict => dict.ContainsKey(reducedListItem.Key))
.Select(dict => dict[reducedListItem.Key]?.ToString() ?? "ERROR")
);
return new KeyValuePair<string, object>(reducedListItem.Key, tempList);
}
)
.ToList();
//从coreList获取字典
核心清单=
核心名单
价值观
.Where(value=>value为IEnumerable&&value.GetType().IsGenericType)
.SelectMany(值=>value作为列表)
.Cast()
.ToList();
//生成结果
麦角新碱=
简化列表
.选择(reducedListItem=>
{
圣殿骑士=
新名单()
{
String.IsNullOrEmpty(reducedListItem.Value)
“!berschrift_FEHLER”
:reducedListItem.Value
};
tempList.AddRange(
口述记录表
.Where(dict=>dict.ContainsKey(reducedListItem.Key))
.Select(dict=>dict[reducedListItem.Key]?.ToString()??“错误”)
);
返回新的KeyValuePair(reducedListItem.Key,tempList);
}
)
.ToList();
由于在第一个语句中收集了所有字典,所以这是更快的
然而,我认为有些东西需要重构,因为如果coreList
只包含列表(这样我们就不需要过滤.Value
属性的类型),那么它的真正类型将是Dictionary
,这对我来说有点异味
但是如果目前没有更多关于您的解决方案的领域知识,我无法建议更好的映射
List<KeyValuePair<String, String>> reducedList = new List<KeyValuePair<string, string>>();
reducedList.Add(new KeyValuePair<string, string>("Id", "1. Heading"));
reducedList.Add(new KeyValuePair<string, string>("Year", "2. Heading"));
reducedList.Add(new KeyValuePair<string, string>("House", "3. Heading"));
reducedList.Add(new KeyValuePair<string, string>("Garage", "4. Heading"));
reducedList.Add(new KeyValuePair<string, string>("Basdlf", "The key does not exist"));
//reducedList.Add(new KeyValuePair<string, string>(null, null));//check for strange data
List<KeyValuePair<String/* Property-Name */, object /* Werte Property-Name */>> ergebnisListe = new List<KeyValuePair<string, object>>();//"" daraus ein Array machen
#region Matching with the reduced list
foreach (KeyValuePair<string, String> reducedListItem in reducedList)
{
List<string> tempList = new List<string>();
tempList.Add(reducedListItem.Value != null && !String.IsNullOrEmpty(reducedListItem.Value) ? reducedListItem.Value : "!Überschrift_FEHLER!");//adding the heading first
#region Itereating through the core list, to get the rest for my data set(actually I need some mapping)
foreach (KeyValuePair<string, Object> item in coreList)
{
#region Filter for my desired list
if (item.Value is IEnumerable && item.Value.GetType().IsGenericType) //matching only for lists, I expect to work with
{
foreach (Dictionary<string, object> subItemListItem in item.Value as List<object>)// I am exspecting some kind of list with "KeyValuePair"'s
{
if (subItemListItem.ContainsKey(reducedListItem.Key))//checking for valid keys
{
//doing something with "subItemListItem.Values" or "Keys" ?!
tempList.Add(subItemListItem[reducedListItem.Key] != null ? subItemListItem[reducedListItem.Key].ToString() : "ERROR");
}
}
}
#endregion
}
#endregion
#region Adding the result to my final list
// adding one column/record
ergebnisListe.Add(new KeyValuePair<string, object>(reducedListItem.Key, tempList.ToArray())); //I need "string[]" first record is the heading and the rest is it's related data
#endregion
}
// Getting dictionaries from coreList
var dictsInCoreList =
coreList
.Values
.Where(value => value is IEnumerable && value.GetType().IsGenericType)
.SelectMany(value => value as List<object>)
.Cast<Dictionary<string, object>>()
.ToList();
// Generate the result
ergebnisListe =
reducedList
.Select(reducedListItem =>
{
var tempList =
new List<string>()
{
String.IsNullOrEmpty(reducedListItem.Value)
? "!Überschrift_FEHLER"
: reducedListItem.Value
};
tempList.AddRange(
dictsInCoreList
.Where(dict => dict.ContainsKey(reducedListItem.Key))
.Select(dict => dict[reducedListItem.Key]?.ToString() ?? "ERROR")
);
return new KeyValuePair<string, object>(reducedListItem.Key, tempList);
}
)
.ToList();