0

So I'm using LINQKIT found here

And I'm trying to create an expression on one of my columns, CID.

The predicate looks like this:

Dim cid As String = "11R"

Dim predicate = PredicateBuilder.[New](Of uv_Manifest).And(Function(x) x.CID = cid)

And it returns a predicate which looks like this:

{x => (CompareString(x.CID, value(FSVendor_RefactoredRepository.ManifestManager+_Closure$__2-0).$VB$Local_cid, False) == 0)}

This returns SQL which looks like this:

SELECT 
    [Extent1].[CID] AS [CID], 
    FROM (SELECT 
    [uv_Manifest].[CID] AS [CID],   
    FROM [dbo].[uv_Manifest] AS [uv_Manifest]) AS [Extent1]
    WHERE ([Extent1].[CID] = @p__linq__0) OR (([Extent1].[CID] IS NULL) AND (@p__linq__0 IS NULL))

When I run this statement in visual studio it times out, whereas if I hardcode the predicate to be like this:

Dim predicate = PredicateBuilder.[New](Of uv_Manifest).And(Function(x) x.CID = "11R")

It works fine and gives me a predicate which looks like this:

{x => (CompareString(x.CID, "11R", False) == 0)}

And the SQL looks like this:

SELECT 
    [Extent1].[CID] AS [CID]
    FROM (SELECT 
    [uv_Manifest].[CID] AS [CID]
    FROM [dbo].[uv_Manifest] AS [uv_Manifest]) AS [Extent1]
    WHERE '11R' = [Extent1].[CID]

And it executes fine.

Here is my line of LINQ:

Dim q = webDataContext.uv_Manifest.AsExpandable().Where(filters)

Why is it when I change the value to be hardcoded it works as expected but when I don't it doesn't create the correct SQL statement and how do I fix this?

Andrew
  • 720
  • 3
  • 9
  • 34
  • In SQL `col = NULL` is `UNKNOWN` That is why it creates `OR (([Extent1].[CID] IS NULL) AND (@p__linq__0 IS NULL))` – Lukasz Szozda Nov 09 '17 at 17:59
  • So at that point it doesnt know what the cid is anymore right? – Andrew Nov 09 '17 at 18:03
  • 1
    Holy crap that's a lot of effort to write a simple SQL script. I don't understand the purpose of this software. Sorry I don't have the answer though! – Jacob H Nov 09 '17 at 18:10
  • We're using entity framework – Andrew Nov 09 '17 at 18:11
  • Right - when the framework doesn't know that the value of `cid` is non-null, it must add the additional test to handle that case. That is no reason for the SQL to timeout, however. BTW, that isn't a function of PredicateBuilder but rather of EF. – NetMage Nov 09 '17 at 21:04

0 Answers0