C# – Issue with Linq2sql query

clinq-to-sql

I'm having trouble with following linq2sql query:

public IEnumerable List(IQueryable<Enquiry> enquiries, Supplier supplier)
{


    IEnumerable result = from e in enquiries
                         let order = supplier==null ? null : e.Orders.Where(f => f.ClientId.Equals(supplier.Id)).FirstOrDefault() 
                         let enquiryUser = e.Client.ClientUsers.First()
                         select new
                         {
                             Id = e.Id,
                             Name = e.Name,
                             PublicId = EnquiryMethods.GetPublicId(e.PublicId),
                             Price = supplier==null ? 0 : EnquiryMethods.GetPrice(e, supplier),
                             What = e.WorkType.DescriptionText,
                             Where = e.EnquiryArea.DescriptionText,
                             Who = e.EnquiryType0.DescriptionText,
                             When = e.EnquiryTime0.DescriptionText,
                             PriceRange = e.EnquiryPrice0.DescriptionText,
                             DisplayPriceRange = e.EnquiryPrice0.Display,
                             NbrOrders = e.Orders.Count(),
                             Description = StringUtils.Nl2Br(e.Description??""),
                             Published = e.EnquiryPublished,
                             HasPurchased = order!=null,
                             BuyerFirstName = order!=null ? enquiryUser.FirstName : "",
                             BuyerLastName = order!=null ? enquiryUser.LastName : "",
                             BuyerPhone = order!=null ? enquiryUser.Phone : "",
                             BuyerCellphone = order!=null ? enquiryUser.Cellphone : "",
                             BuyerEmail = order!=null ? enquiryUser.Email : "",
                             EnquiryMessage = order!=null ? StringUtils.Nl2Br(order.EnquiryMessage??"") : "",
                             OrderId = order!=null ? (Guid?)order.Id : null
                         };

    return result;
}

When passing not-null supplier it works file, but when passing along a null for supplier I get "NullReferecenceException was unhandled by user code".

My theory is that linq2sql insn't to happy with X ? Y: Z operations as it seems that Z ("e.Orders.Where(f => f.ClientId.Equals(supplier.Id)).FirstOrDefault()") Is allways evaluated? I solved this through supplier==null ? Guid.Empty : supplier.Id but I still can't get the rest of the query to work (the order!=null parst breaks, figured this out by commenting all but one).

The weird thing is that e.Orders.Where(f => f.ClientId.Equals(supplier.Id)).FirstOrDefault() gives a null in the case where supplier is non-null and the query works. Is there difference between null and null?

How would you write this?

Best Answer

I have no idea if this will work or not, but try changing:

let order = supplier==null
               ? null
               : e.Orders.Where(f => f.ClientId.Equals(supplier.Id))
                         .FirstOrDefault()

to

let order = e.Orders.Where( f => supplier != null
                                 && f.ClientId.Equals(supplier.Id) )
                    .FirstOrDefault()

since you say it works when the second part is evaluated.