views:

193

answers:

3

I am using VS2008 (no option for VS2010 right now) and am doing a (reverse) Contains to try to match a partial string to a whole string. For instance, the urlRef = http://www2.rivworks.com/feed-test-1/?SSScrollPosition=0 and the defaultUrlDomain = www2.rivworks.com.

IList<vwCompanyDetails> efMatchingUrlCompanyList = null;
...
efMatchingUrlCompanyList = _dbRiv.vwCompanyDetails
                                 .Where(a => urlRef.Contains(a.defaultURLDomain) 
                                          && a.isCompanyDeleted == false)
                                 .ToList();
bool foundCompanyUrl = (efMatchingUrlCompanyList.Count > 0);

Looking at my database, I should get 3 records from this query. Instead, I am getting zero records in efMatchingUrlCompanyList. (edited)

Any ideas or will I have to get all of the company details then do a manual check of each record in a loop?


NOTE:

I added a secondary way of doing this and this works.

var efMatchingUrlCompanyList2 = _dbRiv.vwCompanyDetails.Where(a => a.ClientID != null && a.ClientID > 0 && a.defaultURLDomain != null).ToList();
foreach (vwCompanyDetails myDetail in efMatchingUrlCompanyList2)
{
    if (urlRef.ToLower().Contains(myDetail.defaultURLDomain.ToLower()))
    {
        efMatchingUrlCompanyList.Add(myDetail);
    }
}
foundCompanyUrl = (efMatchingUrlCompanyList.Count > 0);

I added the WHERE clause to shorten down the record set to something I knew would be valid. I will continue playing around with the original to see if I can get it to work as expected.


NOTE 2: I downloaded and am using LINQPad. I connected to my EF model and created this small piece of code:

void Main()
{
    IList<vwCompanyDetails> efMatchingUrlCompanyList = null;
    string urlRef = "http://www2.rivworks.com";
    efMatchingUrlCompanyList = vwCompanyDetails.Where(a => urlRef.Contains(a.defaultURLDomain) && a.isCompanyDeleted == false).ToList();
}

I looked at the generated SQL and this is what it generated (I cut out 90% of the columns for brevity):

-- Region Parameters
DECLARE p__linq__1 NVarChar(17) SET p__linq__1 = 'http://www2.rivworks.com'
-- EndRegion
SELECT 
[Extent1].[CompanyId] AS [CompanyId], 
[Extent1].[CompanyName] AS [CompanyName], 
[Extent1].[isCompanyDeleted] AS [isCompanyDeleted], 
[Extent1].[ClientID] AS [ClientID], 
[Extent1].[defaultURLDomain] AS [defaultURLDomain], 
FROM (SELECT 
      [vwCompanyDetails].[CompanyId] AS [CompanyId], 
      [vwCompanyDetails].[CompanyName] AS [CompanyName], 
      [vwCompanyDetails].[isCompanyDeleted] AS [isCompanyDeleted], 
      [vwCompanyDetails].[ClientID] AS [ClientID], 
      [vwCompanyDetails].[defaultURLDomain] AS [defaultURLDomain], 
      FROM [dbo].[vwCompanyDetails] AS [vwCompanyDetails]) AS [Extent1]
WHERE ((CAST(CHARINDEX([Extent1].[defaultURLDomain], @p__linq__1) AS int)) > 0) 
AND (0 = [Extent1].[isCompanyDeleted])

I copied/pasted this into SQL Mangler and tried to run it. I got an error. I looked at the definition again and low and behold - the p_linq_1 variable DOES NOT have an @ symbol. It is trying to treat it as a cursor instead! I manually added the @ symbol in SQL Mangler and it runs flawlessly!

So, the next question - WHY is it doing this? Is this a known issue?

A: 

I can see no problem with your code. But efMatchingUrlCompanyList should never be null directly after the query - instead it should contain an empty list. If you really find null when you access the variable, you probably have a bug outside of the code included in the question.

Daniel Brückner
Ah right. I should have said zero records as it is a list. `bool foundCompanyUrl = (efMatchingUrlCompanyList.Count > 0);` <-- this is my quick check piece of code so you are right. It is not null - just empty when I expected 3 records.
Keith Barrows
Okay, that's fine. As I said, I would really expect to get the desired result, too. Have you tested String.Conatins() on other tables and columns and with other search strings? Does it work there? At the moment I have SQL server collation and string comparison in mind - that could maybe cause the strings not to match.
Daniel Brückner
A: 

The where statement doesn't return true. Absolutely.

Mendy
Not sure I understand why you are saying this.
Keith Barrows
A: 

Since no one can come forward on the generated SQL I have chosen to use the less efficient route as in my first note. Bummer really...

Keith Barrows