WorknHardr
asked on
LINQ Entity Query Return Null Subtract Error?
I have a simple LINQ Entiry query which returns null because NO table rows exist yet. I would like a more elegant code instead of the one I currently have working. Help.
var incomes = (from i in context.Incomes
where i.UserID == 101
group i by i.UserID into g
select new { Total = g.Sum(s => s.Amount) }).FirstOrDefault();
decimal incomesTotal = 0;
if (incomes != null)
{
incomesTotal = incomes.Total;
}
MyViewModel model = new MyViewModel
{
// totalDiff = (decimal?)incomes.Total ?? 0 - budget.Total, // Not Working Solution
totalDiff = incomesTotal - budget.Total
};
var incomes = (from i in context.Incomes
where i.UserID == 101
group i by i.UserID into g
select new { Total = g.Sum(s => s.Amount) }).FirstOrDefault();
decimal incomesTotal = 0;
if (incomes != null)
{
incomesTotal = incomes.Total;
}
MyViewModel model = new MyViewModel
{
// totalDiff = (decimal?)incomes.Total ?? 0 - budget.Total, // Not Working Solution
totalDiff = incomesTotal - budget.Total
};
ASKER
I still get an error if "Amount" is null.
Error: The cast to value type 'System.Decimal' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
Error: The cast to value type 'System.Decimal' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
MyViewModel model = new MyViewModel
{
totalDiff = context.Incomes
.Where(i => i.UserID == 101)
.Sum(i => i.Amount) - budget.Total
};
Ah, I missed that detail. You can add one more call:
MyViewModel model = new MyViewModel
{
totalDiff = context.Incomes
.Where(i => i.UserID == 101)
.Sum(i => i.Amount)
.DefaultIfEmpty() - budget.Total
};
ASKER
Hmm, still same error. This is the most difficulty I've experienced with LINQ. I had to place the DefaultIfEmpty differently because it doesn't exist the other way and still doesn't work.
MyViewModel model = new MyViewModel
{
totalDiff = context.Incomes
.Where(i => i.UserID == 101)
.DefaultIfEmpty() //<--
.Sum(i => i.Amount) - budget.Total
};
Let's blame it on my sleep deprivation ; )
What is the type of the Amount property?
What is the type of the Amount property?
ASKER
Okay making progress,...
1. changed 'totalDiff' to Nullable
2. added Nullable to query
3. added .GetValueOrDefault(0)
4. Works!
- view returns $-2191.03 which is correct
public Nullable<decimal> totalDiff{ get; set; }
MyViewModel model = new MyViewModel
{
totalDiff = context.Incomes
.Where(i => i.UserID == 101)
.Sum(i => (decimal?)i.Amount)
.GetValueOrDefault(0)
- budget.Total
};
[View]
@Model.totalDiff .Value.ToString("c")
1. changed 'totalDiff' to Nullable
2. added Nullable to query
3. added .GetValueOrDefault(0)
4. Works!
- view returns $-2191.03 which is correct
public Nullable<decimal> totalDiff{ get; set; }
MyViewModel model = new MyViewModel
{
totalDiff = context.Incomes
.Where(i => i.UserID == 101)
.Sum(i => (decimal?)i.Amount)
.GetValueOrDefault(0)
- budget.Total
};
[View]
@Model.totalDiff .Value.ToString("c")
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I like it, thanks again for great help :)
Open in new window