tags:

views:

82

answers:

3

I have the following LINQ expression that's not returning the appropriate 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 lies in the Estimator = (quote.EstimatorID != null && quote.EstimatorID != String.Empty) ? db.Employees.Single(c => c.EmployeeID == quote.EstimatorID).FullName : "Unassigned" part.

I NEVER get it to evalueate to "Unassigned", on the ones that are supposed to, it just returns null. Have I written this wrong?

+1  A: 

Try this and see if you're receiving 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 the ternary expression (!string.IsNullOrEmpty part) with false and see if you reach "Unassigned". I've found that sometimes, when you use a ternary expression in a LINQ query, you have to wrap the whole thing in parentheses.

Jim Schubert
Added a check against a trimmed EstimatorID. I've had single space values create a debugging nightmare in ADO.NET. I believe LINQ to SQL and EF automatically trim, but I wouldn't rely on that.
Jim Schubert
I tried adding yours in, however I received an extremely cryptic error on DataBind when the query was ran.When I put in false however, it ran just fine.
WedTM
That's very weird. I did have one too many opening parentheses and modified my answer around the time you posted. It sounds to me like the Single isn't returning what you'd expect. Maybe change that to `db.Employees.Select(c => c.EmployeeID == quote.EstimatorID).SingleOrDefault()` ?? Is this an option?
Jim Schubert
+1  A: 

I think your expression is correct, although I would advise changing it to !string.IsNullOrEmpty(quote.EstimatorID) for clarity.

Note however that your expression could still return null if db.Employees.Single(c => c.EmployeeID == quote.EstimatorID).FullName returns null.

Mark Byers
A: 

Then it looks like quote.EstimatorID is not null or empty string. Is db.Employees.Single() returning null here? Debug it - put breakpoints into those parts of the expression (putting them on separate lines, if necessary). If you can't do that wrap methods around them that call Debug.WriteLine() or similar.

Evgeny