Linq過濾器涉及varchar(1)字段的問題

[英]Linq filter issue involving a varchar(1) field


I have a field in my database that is varchar(1). I'm not permitted to change it. The only values for this field are 0 or 1.

我的數據庫中有一個varchar(1)字段。我不允許改變它。此字段的唯一值為0或1。

Here is the where clause of the linq query:

這是linq查詢的where子句:

where
g.id_Group == idGroup &&
a.AccountOpen.Value == '1'

My linq query generated the following sql where clause

我的linq查詢生成了以下sql where子句

WHERE ([t1].[id_Group] = 1234) AND (UNICODE([t0].[AccountOpen]) = '1')

'AccountOpen' is the varchar(1) field.

'AccountOpen'是varchar(1)字段。

I changed the where clause to this manually

我手動將where子句更改為this

WHERE ([t1].[id_Group] = 1234) AND ([t0].[AccountOpen] = '1')

The second query returns data rows and the first one does not. How can I get this to work WITHOUT changing the database schema?

第二個查詢返回數據行,第一個查詢不返回。如何在不更改數據庫架構的情況下使其工作?

3 个解决方案

#1


2  

The VS2008 L2S designer incorrectly maps varchar(1) and nvarchar(1) to System.Char. (see http://social.msdn.microsoft.com/Forums/en/linqtosql/thread/ac91e587-6e91-454c-9fa2-bab20b7a258c)

VS2008 L2S設計器錯誤地將varchar(1)和nvarchar(1)映射到System.Char。 (見http://social.msdn.microsoft.com/Forums/en/linqtosql/thread/ac91e587-6e91-454c-9fa2-bab20b7a258c)

You can change the mapping for your [n]varchar(1) columns the L2S designer so it is mapped to a string instead. That way you can do normal string comparisons, and also will not get exceptions when materializing records containing empty [n]varchar(1)s.

您可以更改L2S設計器的[n] varchar(1)列的映射,以便將其映射到字符串。這樣,您可以進行正常的字符串比較,並且在實現包含空[n] varchar(1)的記錄時也不會出現異常。

#2


1  

Have you tried using a string instead of a char for comparison?

您是否嘗試使用字符串而不是字符進行比較?

where 
g.id_Group == idGroup && 
a.AccountOpen.Value == "1"

#3


0  

It's because the UNICODE() function:

這是因為UNICODE()函數:

Returns the integer value, as defined by the Unicode standard, for the first character of the input expression

對於輸入表達式的第一個字符,返回Unicode標准定義的整數值

So if the AccountOpen field contains the value "1", the UNICODE() value for that is 49...so it doesn't satisfy the search condition (49 <> '1')

因此,如果AccountOpen字段包含值“1”,那么它的UNICODE()值為49 ...因此它不滿足搜索條件(49 <>'1')


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/01/20/72a133ef27ed880399f17996821fa44f.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com