LINQ expression is not evaluating correctly
I have the following LINQ expression that does not return a matching response
var query = from quote in db.Quotes
where quote.QuoteStatus == "Estimating" || quote.QuoteStatus == "Rejected"
from emp in db.Employees
where emp.EmployeeID == quote.EmployeeID
orderby quote.QuoteID descending
select new
{
quote.QuoteID,
quote.DateDue,
Company = quote.Company.CompanyName,
Attachments = quote.Attachments.Count,
Employee = emp.FullName,
Estimator = (quote.EstimatorID != null && quote.EstimatorID != String.Empty)
? db.Employees.Single (c => c.EmployeeID == quote.EstimatorID).FullName
: "Unassigned",
Status = quote.QuoteStatus, Priority = quote.Priority
};
The problem is part Estimator = (quote.EstimatorID != null && quote.EstimatorID != String.Empty) ? db.Employees.Single(c => c.EmployeeID == quote.EstimatorID).FullName : "Unassigned"
.
I NEVER return it to the Unassigned category, to the ones it should be, it just returns null
. Did I write it wrong?
a source to share
Try this and see if you get the same values:
Estimator = ((!string.IsNullOrEmpty(quote.EstimatorID) && !string.IsNullOrEmpty(quote.EstimatorID.Trim())
? (db.Employees.Single(c => c.EmployeeID == quote.EstimatorID)).FullName
: "Unassigned")
If that doesn't work, try replacing the check in ternary expression (! String.IsNullOrEmpty part) with a false one and see if you have reached Unassigned. I found that sometimes when you use a ternary expression in a LINQ query, you have to wrap the whole thing in parentheses.
a source to share
Then it looks like this: is quote.EstimatorID
not an empty or empty string. Is db.Employees.Single () returning null here? Debugging it by placing breakpoints on those parts of the expression (placing them on separate lines if necessary). If you can't do these traversal methods around them that call Debug.WriteLine () or similar.
a source to share