IronicMuffin
IronicMuffin

Reputation: 4192

ServiceStack OrmLite - Is it possible to do a group by and have a reference to a list of the non-grouped fields?

It may be I'm still thinking in the Linq2Sql mode, but I'm having a hard time translating this to OrmLite.

I have a customers table and a loyalty card table.

I want to get a list of customers and for each customer, have a list of express cards.

My strategy is to select customers, join to loyalty cards, group by whole customer table, and then map the cards to a single property on customer as a list.

Things are not named by convention, so I don't think I can take advantage of the implicit joins.

Thanks in advance for any help.

Here is the code I have now that doesn't work:

query = query.Join<Customer, LoyaltyCard>((c, lc) => c.CustomerId == lc.CustomerId)
    .GroupBy(x => x).Select((c) => new { c, Cards = ?? What goes here? });

Edit: I thought maybe this method:

var q = db.From<Customer>().Take(1);
q = q.Join<Customer, LoyaltyCard>().Select();
var customer = db.SelectMulti<Customer,LoyaltyCard>(q);

But this is giving me an ArgumentNullException on parameter "key."

Upvotes: 1

Views: 683

Answers (1)

mythz
mythz

Reputation: 143359

It's not clear from the description or your example code what you're after, but you can fix your SelectMulti Query with:

var q = db.From<Customer>()
    .Join<Customer, LoyaltyCard>();

var results = db.SelectMulti<Customer,LoyaltyCard>(q);
foreach (var tuple in results)
{
    Customer customer = tuple.Item1;
    LoyaltyCard custCard = tuple.Item2;
}

Upvotes: 2

Related Questions