Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: LINQ to Entities does not recognize the method DataLength

I have a quick question here. using EF6 model first.

var db = new MyEntities(GetEntityConnectionString());

ObjectContext objectContext = ((IObjectContextAdapter)db).ObjectContext;
ObjectSet<DOCUMENT> objectSet = objectContext.CreateObjectSet<DOCUMENT>();
var results = objectSet.Where("SqlServer.DATALENGTH(it.BINARYCONTENT)>50000");
Assert.IsTrue(results.ToList().Count == 9);

var results2 = objectSet.Where(doc=>System.Data.Objects.SqlClient.SqlFunctions.DataLength( doc.BINARYCONTENT)>50000);
Assert.IsTrue(results2.ToList().Count == 9);

var results3 = db.DOCUMENTS.Where(doc => System.Data.Objects.SqlClient.SqlFunctions.DataLength(doc.BINARYCONTENT) > 50000);
Assert.IsTrue(results3.ToList().Count == 9);

The first assert succeeds, so why do I get the following exception when results2 and results 3 are executed?

An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Additional information: LINQ to Entities does not recognize the method 'System.Nullable`1[System.Int32] DataLength(Byte[])' method, and this method cannot be translated into a store expression.

Is there any way to get the other asserts to succeed?

like image 949
X-Dev Avatar asked Sep 22 '14 05:09

X-Dev


1 Answers

It turns out that the answer is that I'm using the wrong function.

instead of

System.Data.Objects.SqlClient.SqlFunctions.DataLength

I should be using

System.Data.Entity.SqlServer.SqlFunctions.DataLength

located within EntityFramework.SqlServer.dll

like image 156
X-Dev Avatar answered Oct 06 '22 00:10

X-Dev