How to 'Join' an Entity to other Entity?

Posts   
 
    
cmprogrock
User
Posts: 40
Joined: 16-Nov-2008
# Posted on: 06-Jul-2009 03:03:01   

Hi All, I've got my entities showing up nicely & binding to my Telerik DataGrid. (In this case OrderEntities).

Now I need to do the equivalent of a JOIN to get OrderAccountEntites.

I need to use this data in the same DataGrid.

Q: Confused on how is this done (Adapter & Linq).

Q: Am I getting an EntityCollection OrderEntities?(What should I tell DataGrid)

Thanks

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 06-Jul-2009 10:56:35   

Either use a flat list to display the data like a DynamicList or a TypedList.

Or use prefetchPaths to get the related entity and bind to its fields as shown in the following thread: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=16194

cmprogrock
User
Posts: 40
Joined: 16-Nov-2008
# Posted on: 06-Jul-2009 13:21:03   

So In the code given below, changing this to a Dynamic List - means it works like a traditional SQL table - not an object/entity. Thats fine in this case.

How is this done? (I only get TypeListBase or TypedListBase2 as options)



    public void FillAppList(int OrderId)
    {
        EntityCollection<OrderEntity> MyOrders = null;
        using (DataAccessAdapter adapter = new DataAccessAdapter())
        {
            LinqMetaData metaData = new LinqMetaData(adapter);
            var q = from c in metaData.Order
                    where c.Order == OrderId
                    orderby c.Title
                    select c;


            MyOrders = ((ILLBLGenProQuery)q).Execute<EntityCollection<OrderEntity>>();
            this.Grid1.DataSource = MyOrders ;
            this.Grid1.DataBind();
        }
    }


Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 06-Jul-2009 14:26:05   

Please read the manual. And here is the link

cmprogrock
User
Posts: 40
Joined: 16-Nov-2008
# Posted on: 07-Jul-2009 02:31:00   

Can a Dynamic Table be done using LINQ?

Example:


DataAccessAdapter adapter = new DataAccessAdapter();
ResultsetFields fields = new ResultsetFields(3);
fields.DefineField(EmployeeFields.FirstName, 0, "FirstNameManager", "Manager");
fields.DefineField(EmployeeFields.LastName, 1, "LastNameManager", "Manager");
fields.DefineField(EmployeeFields.LastName, 2, "AmountEmployees", "Employee", AggregateFunction.Count);
IRelationPredicateBucket bucket = new RelationPredicateBucket();
bucket.Relations.Add(EmployeeEntity.Relations.EmployeeEntityUsingEmployeeId, "Employee", "Manager", JoinHint.None);

IGroupByCollection groupByClause = new GroupByCollection();
groupByClause.Add(fields[0]);
groupByClause.Add(fields[1]);
DataTable dynamicList = new DataTable();
adapter.FetchTypedList(fields, dynamicList, bucket, 0, null, true, groupByClause);

How is that written using LINQ & LLBLGenProQuery .Execute?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 07-Jul-2009 06:01:53   

This is taken from the LINQ UnitTests that you can download here: http://llblgen.com/pages/downloads.aspx

public void GroupByOnJoinedRelatedEntityWithDefaultIsEmpty()
{
    using(DataAccessAdapter adapter = new DataAccessAdapter())
    {
        LinqMetaData metaData = new LinqMetaData(adapter);
        var q = from c in metaData.Customer
                join o in metaData.Order on c.CustomerId equals o.CustomerId into og
                from o in og.DefaultIfEmpty()
                group o by new
                {
                    c.CustomerId,
                    c.CompanyName
                } into g
                select new
                {
                    Customer = g.Key,
                    LastOrderDate = g.Max(d => d.OrderDate),
                    TotalOrders = g.Count()
                };

        int count = 0;
        foreach(var v in q)
        {
            count++;
            Assert.IsTrue(v.TotalOrders > 0);
        }

        Assert.AreEqual(91, count);
    }
}
David Elizondo | LLBLGen Support Team