Anything similar to EFs Relationship Fixup?

Posts   
 
    
wordracr
User
Posts: 12
Joined: 16-Sep-2009
# Posted on: 19-Oct-2009 18:54:34   

(http://stackoverflow.com/questions/1535443/ef-query-with-conditinal-include/1535618 Does something like this exist?

I was thinking that something like this might help to be able to write multi-relationship level queries w/o the WithPath() route, but rather select new {}

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 20-Oct-2009 04:35:21   

LINQ2LLBL is very poweful. You can do things like:

var q = from c in metaData.Customer
        select new
        {
            c.Country,
            Orders = c.Orders,
            c.CompanyName
        };
LinqMetaData metaData = new LinqMetaData(adapter);
var q = from c in metaData.Customer
        where c.Country != null
        select new
        {
            c.City,
            Contains3 = (from o in c.Orders select o.EmployeeId).Contains(3),
            ConcatenatedResult = Concatenator.Concat((from o in c.Orders select o.EmployeeId).Contains(3).ToString(), c.City)
        };
LinqMetaData metaData = new LinqMetaData(adapter);
var q = from c in metaData.Customer
        where c.Country == "Germany"
        select new
        {
            c.CompanyName,
            c.City,
            Orders = from o in c.Orders
                     select new
                     {
                         OrderDetails = from od in o.OrderDetails
                                        select new
                                        {
                                            od.Product,
                                            od.Quantity,
                                            od.UnitPrice
                                        },
                         o.Employee,
                         o.OrderDate
                     }
        };
var q = from c in metaData.Customer
        where c.Country == "Germany"
        select new
        {
            c.City,
            LastFiveOrders = (from o in c.Orders orderby o.OrderDate descending select new { o.OrderId, o.OrderDate }).Take(3),
            c.CompanyName
        };

Just read the docs and take a look at the linqTests that come with LLBLGen intallation ([LLBLGen Pro Installation folder]/LinqUnitTests).

David Elizondo | LLBLGen Support Team
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39862
Joined: 17-Aug-2003
# Posted on: 20-Oct-2009 09:42:14   

'Include' in EF is called 'PrefetchPath' in LLBLGen Pro. Please also look at: http://www.llblgening.com/archive/2009/10/prefetchpaths-in-depth/

for a lot of examples (in linq and our native api) how to do conditional eager loading.

Frans Bouma | Lead developer LLBLGen Pro
wordracr
User
Posts: 12
Joined: 16-Sep-2009
# Posted on: 20-Oct-2009 15:51:54   

Thanks guys. I'll go ahead and read that page I'm also hoping that the returned anonymous type can be converted into an entity list. What options are available for this?

I'll also need to read through this, but please still respond in case I miss something. http://weblogs.asp.net/fbouma/archive/2008/02/19/developing-linq-to-llblgen-pro-part-13.aspx

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39862
Joined: 17-Aug-2003
# Posted on: 20-Oct-2009 16:57:58   

You can do an in-memory projection to entities from anonymous types of course. But you can't return an anonymous type which contains entity instances. This is a limitation of our current linq provider as our o/r mapper core uses two paths for fetching data: 1 for entities (which uses inheritance etc.) and one for all other projections.

In general you don't need that at all, as you can use prefetch paths, and let the o/r mapper core do the merging / building of the graph for you.

Frans Bouma | Lead developer LLBLGen Pro
wordracr
User
Posts: 12
Joined: 16-Sep-2009
# Posted on: 20-Oct-2009 17:26:22   

could you provide a very small example on how to do the in-memory projection? I don't understand that and can't find a example by searching on that phrase.

Prefetch paths work wonders, yes, but looking for other options to see what's all available.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 21-Oct-2009 10:59:54   

Fetching customers with their orders as follows:

var q = (from c in metaData.Customers
                     where c.Country == "UK"
                     select c).WithPath(p => p.Prefetch(c => c.Orders));

Now q will be holding the entire projected graph.

For example the following code accesses the orders collection of the first returned customer:

var orders = q.ToList()[0].Orders;
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39862
Joined: 17-Aug-2003
# Posted on: 21-Oct-2009 12:00:31   

Additionally, what I meant was:

var q = from c in metaData.Customer select new { c.CustomerId, c.CompanyName /* etc.*/ };

var entities = from v in q.ToList() select new CustomerEntity {CustomerId = v.CustomerId, CompanyName = v.CompanyName /* etc.*};

this does the projection in memory.

Though this is not recommended. I'm not sure why you want to do things yourself if it's built in, so please go for the prefetch paths like the example given by Walaa above and on the llblgening pages.

Frans Bouma | Lead developer LLBLGen Pro