gpt4 book ai didi

c# - 如果集合为空,如何返回默认值?

转载 作者:太空宇宙 更新时间:2023-11-03 21:53:50 28 4
gpt4 key购买 nike

(FeesFee 对象的集合。)

如果 Fees 集合为空,我想将默认值 0 分配给 FeeValue

下面是我目前所做的。但是,我不想新建 Fee 实例。有哪些替代方法可以写这个?谢谢!

from w in Widgets
select new {
FeeValue = w.Fees.DefaultIfEmpty(new Fee()).First().Value, // Value is a Double
};

最佳答案

你的问题有点令人困惑,但我想我知道你在问什么......

像这样:

Widgets
.Select(w =>
new
{
FeeValue = w.Fees.Select(f => f.Value).DefaultIfEmpty(yourDefaultValue)
});

关于c# - 如果集合为空,如何返回默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13188172/

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