C# 查找在ToDictionary()中引发异常的元素的最佳方法
我有一个由一些较大的配置文件填充的项目列表C# 查找在ToDictionary()中引发异常的元素的最佳方法,c#,exception,dictionary,C#,Exception,Dictionary,我有一个由一些较大的配置文件填充的项目列表 List<TextEntrtry> localTextEntries; 此列表将转换为字典: Dictionary<Guid, TextEntry> textEntries; 显然,我的列表包含两个Id相同的元素 我的问题:找出导致异常的元素的最佳方法是什么 (这将允许我生成一条有意义的完整错误消息)在您的集合上运行此命令,以获取具有重复条目的集合: var duplicateEntries = localTextEntri
List<TextEntrtry> localTextEntries;
此列表将转换为字典:
Dictionary<Guid, TextEntry> textEntries;
显然,我的列表包含两个Id相同的元素
我的问题:找出导致异常的元素的最佳方法是什么
(这将允许我生成一条有意义的完整错误消息)在您的集合上运行此命令,以获取具有重复条目的集合:
var duplicateEntries = localTextEntries.GroupBy(k = > k.Id)
.Where(g = > g.Count() > 1)
.Select(g = > g.Key);
您还可以始终添加扩展方法并从源代码中获取不同的值
IEnumerable <TextEntry> distinctList = localTextEntries.DistinctBy(x = > x.Id);
public static IEnumerable<TSource> Distinctify<TSource, TKey>(this IEnumerable<TSource> inSrc_, Func<TSource, TKey> keyFunct_)
{
var uniqueSet = new HashSet<TKey>();
return inSrc_.Where(tmp => uniqueSet.Add(keyFunct_(tmp)));
}
IEnumerable distinctList=localTextEntries.DistinctBy(x=>x.Id);
公共静态IEnumerable differentify(此IEnumerable inSrc_u,Func keyFunct_u)
{
var uniqueSet=new HashSet();
返回inSrc_.Where(tmp=>uniqueSet.Add(keyFunct_.tmp));
}
在您的收藏中运行此选项,以获取具有重复条目的收藏:
var duplicateEntries = localTextEntries.GroupBy(k = > k.Id)
.Where(g = > g.Count() > 1)
.Select(g = > g.Key);
您还可以始终添加扩展方法并从源代码中获取不同的值
IEnumerable <TextEntry> distinctList = localTextEntries.DistinctBy(x = > x.Id);
public static IEnumerable<TSource> Distinctify<TSource, TKey>(this IEnumerable<TSource> inSrc_, Func<TSource, TKey> keyFunct_)
{
var uniqueSet = new HashSet<TKey>();
return inSrc_.Where(tmp => uniqueSet.Add(keyFunct_(tmp)));
}
IEnumerable distinctList=localTextEntries.DistinctBy(x=>x.Id);
公共静态IEnumerable differentify(此IEnumerable inSrc_u,Func keyFunct_u)
{
var uniqueSet=new HashSet();
返回inSrc_.Where(tmp=>uniqueSet.Add(keyFunct_.tmp));
}
您可以将重新写入字典
以使用自己的实现,该实现包括异常消息中的键
:
//TODO come up with a slightly better name
public static Dictionary<TKey, TValue> MyToDictionary<TSource, TKey, TValue>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TSource, TValue> valueSelector,
IEqualityComparer<TKey> comparer)
{
comparer = comparer ?? EqualityComparer<TKey>.Default;
var dictionary = new Dictionary<TKey, TValue>(comparer);
foreach (var item in source)
{
var key = keySelector(item);
try
{
dictionary.Add(key, valueSelector(item));
}
catch (ArgumentException ex)
{
throw new ArgumentException("Missing key: " + key, ex);
}
}
return dictionary;
}
//托多想出一个稍微好一点的名字
公共静态字典MyToDictionary(
这是一个数不清的来源,
Func键选择器,
Func值选择器,
IEqualityComparer(比较器)
{
比较器=比较器??相等比较器。默认值;
var字典=新字典(比较器);
foreach(源中的var项)
{
var键=键选择器(项);
尝试
{
添加(键、值选择器(项));
}
捕获(参数异常)
{
抛出新的ArgumentException(“缺少键:+键,ex”);
}
}
返回字典;
}
您可能希望在不使用比较器或值选择器的情况下创建重载,其中使用了这些参数的默认值
您可能还希望创建一种新类型的
异常
,将键存储为属性,而不是在异常消息中包含键的字符串值(如果对象没有良好的字符串表示形式).您可以重新编写到字典
,以使用自己的实现,该实现包括异常消息中的键
:
//TODO come up with a slightly better name
public static Dictionary<TKey, TValue> MyToDictionary<TSource, TKey, TValue>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
Func<TSource, TValue> valueSelector,
IEqualityComparer<TKey> comparer)
{
comparer = comparer ?? EqualityComparer<TKey>.Default;
var dictionary = new Dictionary<TKey, TValue>(comparer);
foreach (var item in source)
{
var key = keySelector(item);
try
{
dictionary.Add(key, valueSelector(item));
}
catch (ArgumentException ex)
{
throw new ArgumentException("Missing key: " + key, ex);
}
}
return dictionary;
}
//托多想出一个稍微好一点的名字
公共静态字典MyToDictionary(
这是一个数不清的来源,
Func键选择器,
Func值选择器,
IEqualityComparer(比较器)
{
比较器=比较器??相等比较器。默认值;
var字典=新字典(比较器);
foreach(源中的var项)
{
var键=键选择器(项);
尝试
{
添加(键、值选择器(项));
}
捕获(参数异常)
{
抛出新的ArgumentException(“缺少键:+键,ex”);
}
}
返回字典;
}
您可能希望在不使用比较器或值选择器的情况下创建重载,其中使用了这些参数的默认值
您可能还希望创建一种新类型的
异常
,将键存储为属性,而不是在异常消息中包含键的字符串值(如果对象没有良好的字符串表示)。您可以使用group by检查重复或不重复的项。要创建字典,请对分组项运行ToDictionary
方法:
// get repeated items
var repeated = localTextEntries.GroupBy(t => t.Id).Where(g => g.Count() > 1).Select(i => i);
// get not repeated items
var notRepeated = localTextEntries.GroupBy(t => t.Id).Where(g => g.Count() == 1).Select(i => i.First());
// find not repeated items and create a dictionary
var dictFromNotRepeated = localTextEntries.GroupBy(t => t.Id)
.Where(g => g.Count() == 1)
.ToDictionary(g => g.Key, g => g.First());
您可以使用“分组依据”检查重复项或不重复项。要创建字典,请对分组项运行
ToDictionary
方法:
// get repeated items
var repeated = localTextEntries.GroupBy(t => t.Id).Where(g => g.Count() > 1).Select(i => i);
// get not repeated items
var notRepeated = localTextEntries.GroupBy(t => t.Id).Where(g => g.Count() == 1).Select(i => i.First());
// find not repeated items and create a dictionary
var dictFromNotRepeated = localTextEntries.GroupBy(t => t.Id)
.Where(g => g.Count() == 1)
.ToDictionary(g => g.Key, g => g.First());
最后,@Servy的方法对我最有效。我只是选择了一个匹配的重载并添加了一些更好的错误处理:
public static Dictionary<TKey, TSource> ToDictionary2<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IEqualityComparer<TKey> comparer = null)
{
comparer = comparer ?? EqualityComparer<TKey>.Default;
Dictionary<TKey, TSource> dictionary =
new Dictionary<TKey, TSource>(comparer);
foreach (var item in source)
{
var key = keySelector(item);
try
{
dictionary.Add(key, item);
}
catch (Exception ex)
{
string msg = string.Format("Problems with key {0} value {1}",
key,
item);
throw new Exception(msg, ex);
}
}
return dictionary;
}
公共静态字典Todictionary 2(
这是一个数不清的来源,
Func键选择器,
IEqualityComparer比较器=空)
{
比较器=比较器??相等比较器。默认值;
字典=
新字典(比较器);
foreach(源中的var项)
{
var键=键选择器(项);
尝试
{
添加(键、项);
}
捕获(例外情况除外)
{
string msg=string.Format(“键{0}值{1}有问题”,
钥匙
项目);
抛出新异常(msg,ex);
}
}
返回字典;
}
最后@Servy的方法对我最有效。我只是选择了一个匹配的重载并添加了一些更好的错误处理:
public static Dictionary<TKey, TSource> ToDictionary2<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IEqualityComparer<TKey> comparer = null)
{
comparer = comparer ?? EqualityComparer<TKey>.Default;
Dictionary<TKey, TSource> dictionary =
new Dictionary<TKey, TSource>(comparer);
foreach (var item in source)
{
var key = keySelector(item);
try
{
dictionary.Add(key, item);
}
catch (Exception ex)
{
string msg = string.Format("Problems with key {0} value {1}",
key,
item);
throw new Exception(msg, ex);
}
}
return dictionary;
}
公共静态字典到字典2(
这是一个数不清的来源,
Func键选择器,
IEqualityComparer比较器=空)
{
比较器=比较器??相等比较器。默认值;
字典=
新字典(比较器);
foreach(源中的var项)
{
var键=键选择器(项);
尝试
{
添加(键、项);
}
捕获(例外情况除外)
{
string msg=string.Format(“键{0}值{1}有问题”,
钥匙
项目);
抛出新异常(msg,ex);
}
}
返回字典;
}
你不能检查调试吗?将此列表转换为字典的代码在哪里?@evione查看我的最后一段代码:localtextcentries.ToDictionary()
为什么不使用hashset?@Gilad它是更大、更旧的库的一部分。我可以添加错误处理,但不能更改设计。您不能检查调试吗?将此列表转换为字典的代码在哪里?@evione请参阅我的最后一段代码:localtextcentries.ToDictionary()
为什么不使用hashset?@Gilad它是更大、更旧的库的一部分。我可以添加错误句柄