2009-08-12 109 views
2

我有這樣的:爲什麼生成的查詢不同?

Dim compareAddress1 = (From d In db.Addresses Where d.Address1.Equals("a") And _ 
       d.Address2.Equals(Nothing) And _ 
       d.City.Equals(Nothing) And _ 
       d.POBox.Equals(Nothing) And _ 
       d.PostalCode.Equals(Nothing) And _ 
       d.ZipCode.Equals(Nothing)) 

    Dim compareAddress2 = (From d In db.Addresses Where _ 
        d.Address1.Equals(_address.Address1) And _ 
        d.Address2.Equals(_address.Address2) And _ 
        d.City.Equals(_address.City) And _ 
        d.POBox.Equals(_address.POBox) And _ 
        d.PostalCode.Equals(_address.PostalCode) And _ 
        d.ZipCode.Equals(_address.ZipCode)) 

的對象_address所有領域都爲空。

當我使用的對象,我得到這個查詢:

SELECT [t0].[AddressID], [t0].[Address1], [t0].[Address2], [t0].[City], [t0].[CountryProvinceID], [t0].[POBox], [t0].[PostalCode], [t0].[ZipCode] 
    FROM [dbo].[Address] AS [t0] 
    WHERE ([t0].[Address1] = @p0) AND ([t0].[Address2] = @p1) AND ([t0].[City] = @p2) AND ([t0].[POBox] = @p3) AND ([t0].[PostalCode] = @p4) AND ([t0].[ZipCode] = @p5) 
    ------------------------------- 
    @p0 [AnsiString]: a 
    @p1 [AnsiString]: 
    @p2 [AnsiString]: 
    @p3 [AnsiString]: 
    @p4 [AnsiString]: 
    @p5 [AnsiString]: 

當我硬編碼數據,我得到這個查詢:

SELECT [t0].[AddressID], [t0].[Address1], [t0].[Address2], [t0].[City], [t0].[CountryProvinceID], [t0].[POBox], [t0].[PostalCode], [t0].[ZipCode] 
    FROM [dbo].[Address] AS [t0] 
    WHERE ([t0].[Address1] = @p0) AND ([t0].[Address2] IS NULL) AND ([t0].[City] IS NULL) AND ([t0].[POBox] IS NULL) AND ([t0].[PostalCode] IS NULL) AND ([t0].[ZipCode] IS NULL) 
    ------------------------------- 
    @p0 [AnsiString]: a 

人知道爲什麼嗎?以及如何解決我的問題?

回答

2

Scott Guthrie創建了一個add,它在調試器中起作用。

get it here

+0

似乎不工作,我怎麼知道如果DLL加載? – Fredou 2009-08-12 15:32:46

+0

對我來說工作得很好。你有什麼問題? – 2009-08-12 15:41:53

1

給這個一杆...

Dim results = db.Addresses.Where(Function(d) d.Address1.Equals(_address.Address1) And _ 
              d.Address2.Equals(_address.Address2) And _ 
              d.City.Equals(_address.City) And _ 
              d.POBox.Equals(_address.POBox) And _ 
              d.PostalCode.Equals(_address.PostalCode) And _ 
              d.ZipCode.Equals(_address.ZipCode)) 

Console.WriteLine(db.GetCommand(results).CommandText) 

Dim compareAddresses = results.SingleOrDefault() 
+0

似乎不適用於SingleOrDefault – Fredou 2009-08-12 15:34:02

+0

你是對的 - 它不會因爲SingleOrDefault不是IQueryable。你可以用一個Where替換你的代碼 - 這會給你生成的命令。 – 2009-08-12 15:39:55

+0

我剛剛那樣做,很快就更新了我的問題 – Fredou 2009-08-12 15:44:34

0

你運行鍼對SQL服務器2002年,我這個LINQ當對sql 2000運行linq時看到了一些奇怪的行爲(和你的一樣)

相關問題