为什么生成的查询不同?

时间:2009-08-12 15:20:38

标签: asp.net linq-to-sql

我有这个:

    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
谁知道为什么?以及如何解决我的问题?

4 个答案:

答案 0 :(得分:2)

Scott Guthrie创建了一个可在调试器中使用的附加功能。

get it here

答案 1 :(得分:1)

答案 2 :(得分: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()

答案 3 :(得分:0)

你是否正在对sql server 2002运行这个linq我在针对sql 2000运行linq时遇到了一些奇怪的行为(和你的一样)