2012-02-04 155 views
0

嗨,SQL命令可以工作,但EntityFramework的命令卻不行?

如果我運行在我的數據庫:

SELECT * 
FROM 
    Ads AS a 
INNER JOIN AdCategories AS ac 
    on (ac.Id = a.CategoryId) 
INNER JOIN Location AS l 
    on (l.Id = a.UserLocationId) 
WHERE 
    (l.LevelOrder LIKE '2/3/5/%') AND 
    (a.Title LIKE '%' OR a.Description LIKE '%') AND 
    a.InactivatedDate IS NULL AND 
    a.PublishedDate IS NOT NULL AND 
    a.EndDate >= '2012-02-04 17:01:37' AND 
    a.EndDate >= '2011-11-04 16:55:26' AND 
    ac.LevelOrder Like '7/15/33/36_%' 

這將返回2行,但與實體框架

context.CreateQuery<Ad>(sqlCommand, parameters.ToArray()); 
output = ads.ToList(); 

運行相同的命令時,我得到什麼?

這是SQL語句的樣子:

SELECT VALUE a 
FROM 
    Ads AS a 
INNER JOIN AdCategories AS ac 
    on (ac.Id = a.CategoryId) 
INNER JOIN Location AS l 
    on (l.Id = a.UserLocationId) 
WHERE 
    (l.LevelOrder LIKE @locationLevelOrder0) AND 
    (a.Title LIKE @searchS OR a.Description LIKE @searchS) AND 
    a.InactivatedDate IS NULL AND 
    a.PublishedDate IS NOT NULL AND 
    a.EndDate >= @CurrentTime AND 
    a.EndDate >= @fetchAdsTo AND 
    ac.LevelOrder Like @categoryLevelOrder 

的參數如下:

  1. LocationLevelOrder0 = 2/3/5/%
  2. searchS = %
  3. CurrentTime = {2012-02-04 17:31:34}
  4. fetchAdsTo = {2011-11-04 17:31:34}
  5. categoryLevelOrder = 7/15/33/36/_%

爲什麼我沒有獲得與實體框架的任何結果,而我在SQL經理嗎?選擇價值是什麼意思?如果我刪除它會引發異常?

回答

1

在第一個查詢中,您比較了LevelOrder與'7/15/33/36_%',第二個與7/15/33/36/_%是不同的字符串。這是一個轉錄錯誤還是問題?

+0

謝謝,你是對的,如果我運行7/15/33/36/_%,我不會再有任何命中。我嘗試加工的字符串是「7/15/33/36/X」,其中X我可以是任何字符串,maby「7/15/33/36/69/78/99」或「7/15/33/36/100 /158分之152「 。在SQL中做一個特殊的含義?否則它應該只是刪除_? – Banshee 2012-02-04 17:03:41

+0

是的,在SQL中'_'是'LIKE'運算符中只有一個字符的通配符。 – danihp 2012-02-04 17:07:34

+0

好吧,所以這個7/15/33/36/_%我說匹配7/15/33/36/+至少有一個通配符,但是還要賦予字符串更多的通配符?我會盡力排除這個_並且看看它是否能起作用。感謝您的幫助! – Banshee 2012-02-04 18:36:25

相關問題