日期:2014-05-20  浏览次数:20953 次

entity framework如何进行一对多的联合查询???
RT,高人帮下啊

------解决方案--------------------
帮你顶一下
------解决方案--------------------
entity framework没有用过。。。
------解决方案--------------------
do you mean one-to-many join? 

at least you should show whatever you have tried and explain more about what you want
------解决方案--------------------
你能明确的说下你的需求么?

------解决方案--------------------
以下示例针对 SalesOrderHeader 表和 SalesOrderDetail 表执行 GroupJoin 以查找每个客户的订单数。组联接等效于左外部联接,它返回第一个(左侧)数据源的每个元素(即使其他数据源中没有关联元素)。

C# code

using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    ObjectQuery<SalesOrderHeader> orders = AWEntities.SalesOrderHeader;
    ObjectQuery<SalesOrderDetail> details = AWEntities.SalesOrderDetail;

    var query =
        from order in orders
        join detail in details
        on order.SalesOrderID
        equals detail.SalesOrderID into orderGroup
        select new
        {
            CustomerID = order.SalesOrderID,
            OrderCount = orderGroup.Count()
        };

    foreach (var order in query)
    {
        Console.WriteLine("CustomerID: {0}  Orders Count: {1}",
            order.CustomerID,
            order.OrderCount);
    }
}