SelectMany in Linq to entity

Posted by Brazeta on Stack Overflow See other posts from Stack Overflow or by Brazeta
Published on 2010-05-18T16:35:04Z Indexed on 2010/05/18 16:50 UTC
Read the original article Hit count: 552

I was looking at some examples in microsoft site about linq and I see an example that I need to modify!

http://msdn.microsoft.com/en-us/vcsharp/aa336758.aspx#SelectManyCompoundfrom3

public void Linq16() { List customers = GetCustomerList();

var orders =
    from c in customers
    from o in c.Orders
    where o.OrderDate >= new DateTime(1998, 1, 1)
    select new { c.CustomerID, o.OrderID, o.OrderDate };

ObjectDumper.Write(orders);

}

Insted of having a select that retrives the CustomerID, OrderID and OrderDate I want to select the CustomerID and a System.Collection.Generic.List that contains all the orders for that user! Essentially I want to group my orders by CustomerID, but i noticed that linq to entity does not allow a .ToList(object) inside the select.

I want something like this... List customers = GetCustomerList();

var orders =
    from c in customers
    from o in c.Orders
    where o.OrderDate >= new DateTime(1998, 1, 1)
    select new xpto
    {
      TheCostumerID = c.CustomerID, 
      CostumerOrders = o.Select(i=>i.OrderID).ToList(),
    };

...but the .ToList() is a big problem, at least for me.

I'm trying to find out the solution for that but so far I have acomplished nothing!

Please help me.

© Stack Overflow or respective owner

Related posts about linq-to-entities

Related posts about selectmany