The following code reproduces the problem:

namespace Project1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            // Loading configuration section for in-memory database. 
            // See other cases in App.config file.
            var config = DomainConfiguration.Load("Default");
            var domain = Domain.Build(config);

            using (Session.Open(domain))
            {
                using (var transactionScope = Transaction.Open())
                {
                    // Creating new persistent object
                    var helloWorld = new MyEntity
                                         {
                                             Text = "Hello World!"
                                         };
                    // Committing transaction
                    transactionScope.Complete();
                }
            }

            // Reading all persisted objects from another Session
            using (Session.Open(domain))
            {
                using (var transactionScope = Transaction.Open())
                {
                    var result = Query<MyEntity>.All.Select(m => BuildJsonResponse(m));
                    foreach (JsonResponse jsonResponse in result)
                    {
                        Console.WriteLine(jsonResponse.Text);
                    }

                    transactionScope.Complete();
                }
            }
            Console.ReadKey();
        }

        public static JsonResponse BuildJsonResponse(MyEntity entity)
        {
            return new JsonResponse {Text = entity.Text};
        }
    }

    public class JsonResponse
    {
        public string Text { get; set; }
    }
}

The exception is:

Unhandled Exception: System.InvalidOperationException: Unable to translate lambd a expression 'm => BuildJsonResponse(m)' because it requires to materialize enti ty of type 'Project1.Model.MyEntity'. at Xtensive.Storage.Linq.Materialization.ExpressionMaterializer.CreateEntity( IEntityExpression expression, Expression tupleExpression) in c:\TeamCity\buildAg ent\work\6a5d81309423096e\Xtensive.Storage\Xtensive.Storage\Linq\Materialization \ExpressionMaterializer.cs:line 278 at Xtensive.Core.Linq.ExpressionVisitor1.Visit(Expression e) in c:\TeamCity\ buildAgent\work\6a5d81309423096e\Xtensive.Core\Xtensive.Core\Linq\ExpressionVisi tor{TResult}.cs:line 128 at Xtensive.Core.Linq.ExpressionVisitor.VisitExpressionList(ReadOnlyCollectio n1 expressions) in c:\TeamCity\buildAgent\work\6a5d81309423096e\Xtensive.Core\X tensive.Core\Linq\ExpressionVisitor.cs:line 26 at Xtensive.Core.Linq.ExpressionVisitor.VisitMethodCall(MethodCallExpression mc) in c:\TeamCity\buildAgent\work\6a5d81309423096e\Xtensive.Core\Xtensive.Core\ Linq\ExpressionVisitor.cs:line 129 at Xtensive.Core.Linq.ExpressionVisitor1.Visit(Expression e) in c:\TeamCity\ buildAgent\work\6a5d81309423096e\Xtensive.Core\Xtensive.Core\Linq\ExpressionVisi tor{TResult}.cs:line 106 at Xtensive.Core.Linq.ExpressionVisitor.VisitUnary(UnaryExpression u) in c:\T eamCity\buildAgent\work\6a5d81309423096e\Xtensive.Core\Xtensive.Core\Linq\Expres sionVisitor.cs:line 68 at Xtensive.Storage.Linq.Materialization.ExpressionMaterializer.VisitUnary(Un aryExpression u) in c:\TeamCity\buildAgent\work\6a5d81309423096e\Xtensive.Storag e\Xtensive.Storage\Linq\Materialization\ExpressionMaterializer.cs:line 354 at Xtensive.Core.Linq.ExpressionVisitor1.Visit(Expression e) in c:\TeamCity\ buildAgent\work\6a5d81309423096e\Xtensive.Core\Xtensive.Core\Linq\ExpressionVisi tor{TResult}.cs:line 63 at Xtensive.Storage.Linq.Materialization.ExpressionMaterializer.MakeLambda(Ex pression expression, TranslatorContext context) in c:\TeamCity\buildAgent\work\6 a5d81309423096e\Xtensive.Storage\Xtensive.Storage\Linq\Materialization\Expressio nMaterializer.cs:line 42 at Xtensive.Storage.Linq.Translator.ProcessProjectionElement(Expression body) in c:\TeamCity\buildAgent\work\6a5d81309423096e\Xtensive.Storage\Xtensive.Stora ge\Linq\Translator.Expressions.cs:line 586 at Xtensive.Storage.Linq.Translator.VisitLambda(LambdaExpression le) in c:\Te amCity\buildAgent\work\6a5d81309423096e\Xtensive.Storage\Xtensive.Storage\Linq\T ranslator.Expressions.cs:line 115 at Xtensive.Storage.Linq.Translator.BuildProjection(LambdaExpression le) in c :\TeamCity\buildAgent\work\6a5d81309423096e\Xtensive.Storage\Xtensive.Storage\Li nq\Translator.Queryable.cs:line 901 at Xtensive.Storage.Linq.Translator.VisitSelect(Expression expression, Lambda Expression le) in c:\TeamCity\buildAgent\work\6a5d81309423096e\Xtensive.Storage\ Xtensive.Storage\Linq\Translator.Queryable.cs:line 892 at Xtensive.Storage.Linq.Translator.VisitQueryableMethod(MethodCallExpression mc, QueryableMethodKind methodKind) in c:\TeamCity\buildAgent\work\6a5d81309423 096e\Xtensive.Storage\Xtensive.Storage\Linq\Translator.Queryable.cs:line 181 at Xtensive.Core.Linq.QueryableVisitor.VisitMethodCall(MethodCallExpression m c) in c:\TeamCity\buildAgent\work\6a5d81309423096e\Xtensive.Core\Xtensive.Core\L inq\QueryableVisitor.cs:line 34 at Xtensive.Storage.Linq.Translator.VisitMethodCall(MethodCallExpression mc) in c:\TeamCity\buildAgent\work\6a5d81309423096e\Xtensive.Storage\Xtensive.Storag e\Linq\Translator.Expressions.cs:line 257 at Xtensive.Core.Linq.ExpressionVisitor1.Visit(Expression e) in c:\TeamCity\ buildAgent\work\6a5d81309423096e\Xtensive.Core\Xtensive.Core\Linq\ExpressionVisi tor{TResult}.cs:line 106 at Xtensive.Storage.Linq.Translator.Visit(Expression e) in c:\TeamCity\buildA gent\work\6a5d81309423096e\Xtensive.Storage\Xtensive.Storage\Linq\Translator.Exp ressions.cs:line 74 at Xtensive.Storage.Linq.Translator.Translate[TResult]() in c:\TeamCity\build Agent\work\6a5d81309423096e\Xtensive.Storage\Xtensive.Storage\Linq\Translator.Ma terialization.cs:line 32 at Xtensive.Storage.Linq.QueryProvider.Translate[TResult](Expression expressi on) in c:\TeamCity\buildAgent\work\6a5d81309423096e\Xtensive.Storage\Xtensive.St orage\Linq\QueryProvider.cs:line 65 at Xtensive.Storage.Linq.QueryProvider.Execute[TResult](Expression expression ) in c:\TeamCity\buildAgent\work\6a5d81309423096e\Xtensive.Storage\Xtensive.Stor age\Linq\QueryProvider.cs:line 59 at Xtensive.Storage.Linq.Queryable1.GetEnumerator() in c:\TeamCity\buildAgen t\work\6a5d81309423096e\Xtensive.Storage\Xtensive.Storage\Linq\Queryable.cs:line 76

This thread was imported from our support forum. The original discussion may contain more detailed answer. Original topic by Rogn.

asked Dec 12 '09 at 20:24

Editor's gravatar image

Editor
46154156157


One Answer:

The problem appear because you're trying to "make" LINQ translator in DO4 to translate .Select(m => BuildJsonResponse(m)) to SQL - obviously, this can't be achieved "by default", since we know nothing about this method.

To resolve the issue, you must fix the query:

...
var result = Query<MyEntity>.All.AsEnumerable().Select(m => BuildJsonResponse(m));
...

Another alternative:

...
var result = Query<MyEntity>.All.ToList().Select(m => BuildJsonResponse(m));
...

P.S. DO4 allows you to write custom LINQ translation rules. This part of Manual explains how to achieve this. But I feel this is impossible to translate BuildJsonResponse to SQL - the method is relatively complex.

answered Dec 12 '09 at 22:44

Alex%20Yakunin's gravatar image

Alex Yakunin
29714412

Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA