views:

20

answers:

1

Here are three classes in my domain:

public class Quote : IEntity, IAggregateRoot {
    public int QuoteId { get; set; }
    public IEnumerable<Price> Prices { get; set; }
}

public class Price : IEntity {
    public int PriceId { get; set; }
    public Carrier Carrier { get; set; }
    public decimal? Price { get; set; }
    public Quote Quote { get; set; }
}

public class Carrier : IEntity, IAggregateRoot {
    public int CarrierId { get; set; }
    public string Name { get; set; }
}

I want to be able to select a projection based on the Prices in the Quote. The return type should be an IEnumerable<[anonymous object]>. I have to start the query from the Quote because it is the root domain object. Here is what I have so far:

session.Linq<Quote>()
    .Expand("Prices")
    .Where(q => q.QuoteId == 1)
    .Select(q => {
        //this is where I don't know what to do.
        //Maybe somthing like this:
        return q.Prices.Select(p => {
            new { CustomerName = p.Customer.Name, Price = p.Price }
        });
    });

The mappings would be:

  • Quote.Prices > HasMany (one-to-many)
  • Price.Quote > References (many-to-one)
  • Price.Carrier > References (one-to-one)
A: 

I found my answer. I completely forgot about the SelectMany Linq expression. Here is my solution.

session.Linq<Quote>()
    .Where(q => q.QuoteId == 1)
    .SelectMany(q => q.Prices, (q, p) => new { CustomerName = p.Customer.Name });
Wili