質問

I'm using VS2008, .NET 3,5, Entity Framework 3.5, Sql Server Compact 3.5 SP2. SQL SC 3.5 is accessed using EF, with Database first approach.

I'm using a simple query (linq and EF) that should use an existing compound index for columns StanjeId (tinyint) and ArtiklId (int).

var compQuery2 = from art in MobileDb.Artikl
                             where art.Stanje.StanjeId == (byte)1
                             && art.ArtiklId == tmp1
                             select art;

var quer1 = MobileDb.Artikl.Where(a => a.Stanje.StanjeId == (byte)1 && a.ArtiklId == tmp1);

The generated query using (compQuery2 as System.Data.Objects.ObjectQuery).ToTraceString() is:

SELECT 
1 AS [C1], 
[Extent1].[ArtiklGuid] AS [ArtiklGuid], 
.
.
[Extent1].[StanjeId] AS [StanjeId], 
[Extent1].[ZemljaPorijeklaDrzavaGuid] AS [ZemljaPorijeklaDrzavaGuid]
FROM [Artikl] AS [Extent1]
WHERE (1 = ( CAST( [Extent1].[StanjeId] AS int))) AND ([Extent1].[ArtiklId] = @p__linq__4)

The problem is that the generated query uses cast to int in the where part of the query for column StanjeId although StanjeId is of type tinyint (byte equivalent). That causes the SQL SC 3.5 not to use index seek, but a very slow table scan (table has >1M records).

How to get EF 3.5 to not use CAST as int in the where part the generated SQL query?

役に立ちましたか?

解決

try this operator
Byte.CompareTo(Byte) , art.Stanje.StanjeId.CompareTo(one) == 0 ,
even if this explicitly casts , we will have to start looking into source code for diff methods.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top