小编Tom*_*any的帖子

调试时,可以查看导致LINQ异常的对象吗?

可以说我有以下代码:

IDictionary<int, int> itemPriceDict = loadItemPriceDictionary();
IList<IRow> dbItems = loadItemsFromDatabase();

IList<ItemDTO> itemDTOs = dbItems
    .Select(dbItem => new ItemDTO()
    {
        Id = dbItem.Id,
        Label = dbItem.Label,
        PriceTag = itemPriceDict[dbItem.Id] // Possible KeyNotFoundException
    })
    .ToList();
Run Code Online (Sandbox Code Playgroud)

当给定的dbItem不存在给定的价格标签时,有时会出现KeyNotFound异常。

现在,当在Visual Studio中调试并引发异常时,您可以看到StackTrace,TargetSite,它向您显示触发该异常的代码行,但是 可以找出是哪个对象(dbItem)导致了异常并在Debugger中显示其数据?例如在“监视”窗口中?

我想要:

  1. 要么知道词典中没有哪个键
  2. 或者更好的是知道键以及在Select中处理的dbItem

但无需添加或修改任何代码。

PS:我知道我可以将代码重写为循环,但是我不想这样做。

c# linq debugging exception visual-studio

3
推荐指数
1
解决办法
380
查看次数

标签 统计

c# ×1

debugging ×1

exception ×1

linq ×1

visual-studio ×1