views:

30

answers:

2

I have a 1 to Many relationship between tblBusiness and tblPhone. For a specific BusinessID I am trying to return a single string of information to bind to a textbox. Below is my latest attempt along with the Generated SQL from that same LINQ. No matter WHAT I have tried it returns NULL unless there is a value for all 3 fields.

What am I doing wrong? Thanks!


First the LINQ:

using (var context = ConnectDataContext.Create())
{
   context.Log = Console.Out;

   var business = from businesse in context.tblBusinesses
                 where businesse.BusinessID == businessID
                select businesse.BusinessName 
                     + businesse.ContactName 
                     + businesse.tblPhones.Select(p=>p.PhoneNumber)
                                          .FirstOrDefault() 
                                         ?? string.Empty;

            return business.Single();
}

Now the SQL:

SELECT [t2].[value]
FROM (
    SELECT COALESCE(([t0].[BusinessName] + [t0].[ContactName]) + ((
        SELECT TOP (1) [t1].[PhoneNumber]
        FROM [dbo].[tblPhone] AS [t1]
        WHERE [t1].[BusinessID] = [t0].[BusinessID]
        )),@p0) AS [value], [t0].[BusinessID]
    FROM [dbo].[tblBusiness] AS [t0]
    ) AS [t2]
WHERE [t2].[BusinessID] = @p1
    -- @p0: Input NVarChar (Size = 1; Prec = 0; Scale = 0) [ ]
    -- @p1: Input Int (Size = 0; Prec = 0; Scale = 0) [118]
A: 

Have we tried:

var business = from businesse in context.tblBusinesses 
             where businesse.BusinessID == businessID 
            select (businesse.BusinessName ?? string.Empty)
                 + (businesse.ContactName  ?? string.Empty)
                 + (businesse.tblPhones.Select(p=>p.PhoneNumber) 
                                      .FirstOrDefault()  
                                     ?? string.Empty); 

Of course, I really should ask, "what exactly are you after?". The three phone number concatenated together? The first of thoses that non-null? For the latter, try:

var business = from businesse in context.tblBusinesses 
             where businesse.BusinessID == businessID 
            select businesse.BusinessName ?? 
                   businesse.ContactName  ?? 
                   businesse.tblPhones.Select(p=>p.PhoneNumber) 
                                      .FirstOrDefault()  
                                     ?? string.Empty; 
James Curran
+1  A: 

I'm a LINQ to Entities man myself, and I'm not sure how much string manipulation the LINQ to SQL provider supports, so I would do the string concatenation and null testing outside of the LINQ context:

var business = from businesse in context.tblBusinesses 
               where businesse.BusinessID == businessID 
               select new
               {
                   businesse.BusinessName,
                   businesse.ContactName,
                   Phone = businesse.tblPhones.Select(p=>p.PhoneNumber)
                       .FirstOrDefault() ?? string.Empty
               }.Single();
return (business.BusinessName ?? string.Empty) +
    (business.ContactName ?? string.Empty) +
    (business.Phone ?? string.Empty);

The problem with the original query is that nulls propogate, so "blah" + NULL is NULL.

Stephen Cleary