我得到了“丑陋”的linq到SQL查询:
Dim f = (From d In db.Addresses Where ((_address.Address1 Is Nothing AndAlso d.Address1.Equals(Nothing)) OrElse (_address.Address1 IsNot Nothing AndAlso d.Address1.Equals(_address.Address1))) And _
((_address.Address2 Is Nothing AndAlso d.Address2.Equals(Nothing)) OrElse (_address.Address2 IsNot Nothing AndAlso d.Address2.Equals(_address.Address2))) And _
((_address.City Is Nothing AndAlso d.City.Equals(Nothing)) OrElse (_address.City IsNot Nothing AndAlso d.City.Equals(_address.City))) And _
((_address.POBox Is Nothing AndAlso d.POBox.Equals(Nothing)) OrElse (_address.POBox IsNot Nothing AndAlso d.POBox.Equals(_address.POBox))) And _
((_address.PostalCode Is Nothing AndAlso d.PostalCode.Equals(Nothing)) OrElse (_address.PostalCode IsNot Nothing AndAlso d.PostalCode.Equals(_address.PostalCode))) And _
((_address.ZipCode Is Nothing AndAlso d.ZipCode.Equals(Nothing)) OrElse (_address.ZipCode IsNot Nothing AndAlso d.ZipCode.Equals(_address.ZipCode))) And _
((_address.CountryProvinceID Is Nothing AndAlso d.CountryProvinceID.Equals(Nothing)) OrElse (_address.CountryProvinceID IsNot Nothing AndAlso d.CountryProvinceID.Equals(_address.CountryProvinceID))))
所以它生成了这个查询
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] IS NULL) AND ([t0].[Address2] = @p0) AND ([t0].[City] IS NULL) AND ([t0].[POBox] IS NULL) AND ([t0].[PostalCode] = @p1) AND ([t0].[ZipCode] IS NULL) AND ([t0].[CountryProvinceID] IS NULL)
-------------------------------
@p0 [AnsiString]: a
@p1 [AnsiString]: a
有没有办法清理它,只是一点点?
它的作用是在表格中检查地址是否与用户输入的地址相匹配
答案 0 :(得分:0)
你发布的内容并不像我在检查空字符串一样,除非你指的是PostalCode并且@ p0是空字符串。
如果是这样的话,我会这样做:
(From d In db.Addresses Where (d.PostalCode.Equals(Nothing)
OrElse (d.PostalCode.Equals(_address.PostalCode)
AndAlso Not String.IsNullOrEmpty(d.PostalCode))))
答案 1 :(得分:0)
haaa ....解决方案是......
Dim f = (From d In db.Addresses Where Object.Equals(d.Address1, _address.Address1) And _
Object.Equals(d.Address2, _address.Address2) And _
Object.Equals(d.City, _address.City) And _
Object.Equals(d.POBox, _address.POBox) And _
Object.Equals(d.PostalCode, _address.PostalCode) And _
Object.Equals(d.ZipCode, _address.ZipCode) And _
Object.Equals(d.CountryProvinceID, _address.CountryProvinceID))