Query only works when using anonymous types

Posts   
 
    
Rohland
User
Posts: 14
Joined: 25-Jul-2007
# Posted on: 18-Jan-2010 17:22:29   

Hi,

I wonder if someone could assist me solving this particular problem I have been having. I believe the following code should work but I get the error posted underneath the code sample:



            var result = from l in context.TodoList
                         orderby l.Position ascending, l.CreatedOn descending
                         select new TodoListSummary
                         {
                             Id = l.Id,
                             Title = l.Title,
                             Description = l.Description,
                             Position = l.Position,
                             CreatedOn = l.CreatedOn,
                             Tasks = from t in l.Todos
                                     select new TaskSummary
                                     {
                                         Id = t.Id,
                                         Title = t.Title,
                                         Position = t.Position,
                                         CreatedOn = t.CreatedOn,
                                         Completed = t.Completed == 1,
                                         CompletedDate = t.CompletedDate,
                                         Company = t.Company,
                                         User = t.User,
                                         TimeSpentInMinutes = t.Timeentries.Sum(y => y.TimeInMinutes),
                                         CommentCount = t.Comments.Count()
                                     }
                         };


Error:

The multi-part identifier "LPLA_3.id" could not be bound. The multi-part identifier "LPLA_3.name" could not be bound. The multi-part identifier "LPLA_4.userID" could not be bound. The multi-part identifier "LPLA_4.firstname" could not be bound. The multi-part identifier "LPLA_4.lastname" could not be bound. The multi-part identifier "LPLA_4.fullname" could not be bound. The multi-part identifier "LPLA_4.email" could not be bound. The multi-part identifier "LPLA_4.companyId" could not be bound. The multi-part identifier "LPLA_4.createdOn" could not be bound. The multi-part identifier "LPLA_4.createdBy" could not be bound. The multi-part identifier "LPLA_4.modifiedOn" could not be bound. The multi-part identifier "LPLA_4.modifiedBy" could not be bound. The multi-part identifier "LPLA_4.createdUserId" could not be bound. The multi-part identifier "LPLA_4.modifiedUserId" could not be bound.

Essentially it is failing to include t.Company and t.User entities in the query it is producing. The strange thing is that if I rewrite the query such that anonymous types are used it works.



            var result = from l in context.TodoList
                         orderby l.Position ascending, l.CreatedOn descending
                         select new
                         {
                             Id = l.Id,
                             Title = l.Title,
                             Description = l.Description,
                             Position = l.Position,
                             CreatedOn = l.CreatedOn,
                             Tasks = from t in l.Todos
                                     select new
                                     {
                                         Id = t.Id,
                                         Title = t.Title,
                                         Position = t.Position,
                                         CreatedOn = t.CreatedOn,
                                         Completed = t.Completed == 1,
                                         CompletedDate = t.CompletedDate,
                                         Company = t.Company,
                                         User = t.User,
                                         TimeSpentInMinutes = t.Timeentries.Sum(y => y.TimeInMinutes),
                                         CommentCount = t.Comments.Count()
                                     }
                         };


I recently upgraded the runtime libraries to build 12242009. I am obviously using LLBLGEN Pro 2.6.

I find this issue even more strange considering the following from the help documentation:

Storing an entity in an anonymous type field isn't supported, as the entity itself isn't materialized so it's not storable as a value inside the anonymous object.

Any help would be greatly appreciated.

Regards, Rohland

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39865
Joined: 17-Aug-2003
# Posted on: 18-Jan-2010 17:34:28   

Please simplify the query to the bare minimum what fails, and use a well known database like northwind or adventureworks so we can build an easy reprocase. Debugging linq queries is very difficult, so the smallest query which fails is best and also if you use custom classes instead of entities (the code suggests that) please provide these classes (and if these classes are big, obviously create dummy ones which only have property definitions)

(about the help quote). That refers to: var q = from c in metaData.Customer select new {c, c.Orders.Count};

which isn't supported, as the materialization of the anonymous type with 'c' isn't possible as 'c' isn't materialized into an object when the anonymous type instances are created.

Also, the nested entity references could work, because they're executed as nested queries (so they're different from the example query I gave above). It might be there's a glitch with fixed types.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39865
Joined: 17-Aug-2003
# Posted on: 18-Jan-2010 17:47:16   

I constructed a repro case:


[Test]
public void EntityMaterializationThroughNestedQueriesTest()
{
    using(DataAccessAdapter adapter = new DataAccessAdapter())
    {
        LinqMetaData metaData = new LinqMetaData(adapter);
        var q = from o in metaData.Order
                where o.EmployeeId.Value==2
                select new Order2 { OId = o.OrderId, Customer = o.Customer };

        foreach(var v in q)
        {
            Console.WriteLine("OrderId: {0}. CustomerID: {1}", v.OId, v.Customer.CustomerId);
        }
    }
}

fails

but


[Test]
public void EntityMaterializationThroughNestedQueriesTest()
{
    using(DataAccessAdapter adapter = new DataAccessAdapter())
    {
        LinqMetaData metaData = new LinqMetaData(adapter);
        var q = from o in metaData.Order
                where o.EmployeeId.Value==2
                select new { OId = o.OrderId, Customer = o.Customer };

        foreach(var v in q)
        {
            Console.WriteLine("OrderId: {0}. CustomerID: {1}", v.OId, v.Customer.CustomerId);
        }
    }
}

works. Interesting. Will look into it.

Frans Bouma | Lead developer LLBLGen Pro
Rohland
User
Posts: 14
Joined: 25-Jul-2007
# Posted on: 19-Jan-2010 07:39:53   

Sorry for not posting a simpler case. I failed to mention that I had tested this with a number of different projects and queries.

Any idea what is causing the issue?

Edit: ?Do you have any suggested workarounds for circumventing this problem for now?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39865
Joined: 17-Aug-2003
# Posted on: 19-Jan-2010 09:36:05   

Rohland wrote:

Sorry for not posting a simpler case. I failed to mention that I had tested this with a number of different projects and queries.

Any idea what is causing the issue?

Edit: ?Do you have any suggested workarounds for circumventing this problem for now?

Only by using an anonymous type.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39865
Joined: 17-Aug-2003
# Posted on: 19-Jan-2010 12:13:01   

Fixed it. I'll get back to you when the fix is available (as more fixes in the linq provider are made, so we just have to add 1 new build instead of several wink )

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39865
Joined: 17-Aug-2003
# Posted on: 19-Jan-2010 12:21:07   

Please see this post for the fixed linq provider. It also fixes the issue reported in that thread: http://www.llblgen.com/tinyforum/GotoMessage.aspx?MessageID=96373&ThreadID=17196

Frans Bouma | Lead developer LLBLGen Pro
Rohland
User
Posts: 14
Joined: 25-Jul-2007
# Posted on: 19-Jan-2010 12:33:30   

Great, thanks very much!