2015-05-11 23 views
5

nella mia tabella DB SQL Server, ho il tipo di colonna binary.Entity Framework 'ArrayIndex' non è supportato in LINQ alle entità

Non riesco a eseguire la query, perché?

var data = (from x in model.MyTable 
      where x.BinaryColumn[0] == 1 
      select x).FirstOrDefault(); 

ottengo l'errore

+0

Qual è il tipo di 'BinaryColumn'? – haim770

+0

Poiché Entity Framework non sa come tradurlo in TSQL. – xanatos

+0

haim770 >> è il tipo 'byte []' in C# – Tony

risposta

1

The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities Se siete disposti a modificare la tua ricerca un po ', questo dovrebbe funzionare:

var data = (from x in model.MyTable 
      where SqlFunctions.CharIndex(new byte[] { 1 }, x.BinaryColumn) == 1 
      select x).FirstOrDefault(); 

Vedi MSDN

+0

restituire '1', non' 0' Penso, perché SQL conta da 1 – xanatos

+0

@xanatos, Fisso. Grazie. – haim770

3

Nel TSQL la funzione SUBSTRING può essere utilizzato su binary/varbinary.

qualche definire:

[DbFunction("SqlServer", "SUBSTRING")] 
public static byte[] SubString(byte[] field, int start, int length) 
{ 
    throw new NotSupportedException("Direct calls are not supported."); 
} 

poi

var data = (from x in model.MyTable 
      where Substring(x.BinaryColumn, 1, 1) == new byte[] { 1 } 
      select x).FirstOrDefault(); 
Problemi correlati