error: LINQ to Entities does not recognize the method DataLength

c# entity-framework entity-framework-6 linq linq-to-entities

Question

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?

1
6
9/22/2014 5:36:53 AM

Accepted Answer

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

8
10/1/2014 11:27:15 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow