views:

175

answers:

5

This simple Linq query:

from c in mycontext.Customers
join o in mycontext.Orders on c.CustomerId equals o.CustomerId
where o.Status == 1
select new {c, o}

will result in

List<{c:Customer, o:Order}>

after calling ToList().

What's the easiest way of converting this anonymously typed list into a list of customers (List<Customer>)?

EDIT: I need the orders for an extra condition, I've changed my original question.

+1  A: 

very basic approach, as you explecitely asked "What's the easiest way of converting this anonymously typed [...]":

var anonymousEnumerable = from c in mycontext.Customers
                          join o in mycontext.Orders on c.CustomerId equals o.CustomerId
                          select new
                          {
                              c,
                              o
                          };
var typedEnumerable = anonymousList.Select(item => item.c).Distinct(); // either referenceCheck or you supply an IEqualityComparer<Customer>-implementation

maybe you can give us some more information an what you want exactly to achieve!

Andreas Niedermair
I want to convert the result of my Linq query into a List<Customer> as simply as possible.
Gerrie Schenck
for **this** list, i gave you the answer ... if you'd like a more general (aka generic) answer, there might be another solution :)
Andreas Niedermair
+6  A: 
result.Select(o => o.Customer).ToList();

Is this what you mean?

Roel
This would return each customer `X` times, where `X` is the number of orders associated to each customer. Your answer is not wrong but maybe he wanted the distinct customers, and he better know this example doesn't do it.
Alex Bagnolini
Good point. The question is why is there the join with Orders? Anyway you can still call .Distinct() if necessary...
Roel
this isn't the simplest way though
John Nicholas
This worked for me, thanks Roel.
Gerrie Schenck
A: 

Do you need both the properties? If so step through the list and instantiate each curstomer...

Something like

 List<Customer> customers = new List<Customer>();
 foreach(item in linqQuery.ToList())
 {

     customers.Add(item.c);
     //do something with the Order here...
 }
Paul
You don't need the `.ToList()`. Actually you wouldn't need 4 lines to do it, but it's a matter of choice.
Alex Bagnolini
+1  A: 

why not just use .ToList<Customers>()

and don't select the orders - you don't need them after the join.

List<Customer> custList =  (from c in mycontext.Customers
    join o in mycontext.Orders on c.CustomerId equals o.CustomerId
    where o.Status == 1
    select c).ToList<Customer>();
John Nicholas
A: 
var ledger = from c in mycontext.Customers
                 join o in mycontext.Orders on c.CustomerId equals o.CustomerId
                 where o.Status == 1
                 select new {c, o};

var customers = (from row in ledger select row.Customer).Distinct().ToList();

That would be my sorta bidding on a solution (inclusive mispellings etc.) :)

cyberzed