Reputation: 9953
(Fees
is a collection of Fee
objects.)
If the Fees collection is empty, I want to assign a default value of 0
to FeeValue
.
Below is what I currently do. However, I'd prefer to not new up an instance of Fee. What are some alternative ways to write this? Thanks!
from w in Widgets
select new {
FeeValue = w.Fees.DefaultIfEmpty(new Fee()).First().Value, // Value is a Double
};
Upvotes: 1
Views: 417
Reputation: 31
If you want to avoid the empty Select() this should work as well:
Widgets
.Select(w =>
new
{
FeeValue = w.Fees.DefaultIfEmpty(yourDefaultValue).First()
});
Upvotes: 0
Reputation: 25628
Your questions is a bit confusing, but I think I know what you're asking...
Something like this:
Widgets
.Select(w =>
new
{
FeeValue = w.Fees.Select(f => f.Value).DefaultIfEmpty(yourDefaultValue)
});
Upvotes: 4