(Fees
是Fee
對象的集合。)如果收集爲空,我該如何返回默認值?
如果費徵收是空的,我要的0
默認值分配給FeeValue
。
以下是我目前的工作。但是,我寧願不要新的了一個Fee的實例。有什麼其他的方法來寫這個?謝謝!
from w in Widgets
select new {
FeeValue = w.Fees.DefaultIfEmpty(new Fee()).First().Value, // Value is a Double
};
(Fees
是Fee
對象的集合。)如果收集爲空,我該如何返回默認值?
如果費徵收是空的,我要的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)
});
如果你想避免空選擇()這應該也是如此:
Widgets
.Select(w =>
new
{
FeeValue = w.Fees.DefaultIfEmpty(yourDefaultValue).First()
});
費用是Fee對象的集合。對不起,沒有更清楚。如果不是null,我需要返回屬性「Value」。所以我不得不改變這個答案是:'w.Fees.DefaultIfEmpty(new Fee())。First()。Value' ...這匹配我的原始嘗試。 –
你有正確的想法。爲了工作,我需要這樣做:'FeeValue = w.Fees.Select(f => f.Value).DefaultIfEmpty(0).First()' –