gpt4 book ai didi

c# - 如何处理 LINQ Select 语句中的异常

转载 作者:行者123 更新时间:2023-11-30 12:18:30 24 4
gpt4 key购买 nike

我有一个 LINQ 查询如下

 m_FOO = rawcollection.Select(p=> p.Split(' ')).Select(p =>
{
int thing = 0;

try
{
thing = CalculationThatCanFail(p[1]);
}
catch{}
return new { Test = p[0], FooThing = thing};
})
.GroupBy(p => p.Test)
.ToDictionary(p => p.Key, s => s.Select(q => q.FooThing).ToList());

因此,CalculationThatCanFail 有时会抛出。我不想先放入 null 然后再用另一个 Where 语句过滤掉它,垃圾值同样是 Not Acceptable 。有谁知道如何干净地处理这个?谢谢。

编辑:双重 Select 语句是有充分理由的。为简洁起见编辑了此示例

最佳答案

我不清楚你的意思是你不想对 FooThing 使用 null 或者你不想使用 null 用于整个匿名类型的对象。无论如何,这符合要求吗?

 m_FOO = rawcollection.Select(p=> p.Split(' ')).Select(p =>
{
int thing = 0;

try
{
thing = CalculationThatCanFail(p[1]);
return new { Test = p[0], FooThing = thing};
}
catch
{
return null;
}
})
.Where(p => p != null)
.GroupBy(p => p.Test)
.ToDictionary(p => p.Key, s => s.Select(q => q.FooThing).ToList());

关于c# - 如何处理 LINQ Select 语句中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2158722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com