1

I'm trying to do a query to generate packing slips, and need general order info, as well as how many items the order contains.

Here's my basic query:

var orders = (from cert in _orderRepository.GetAll()
                           where !cert.PrintedPackSlip
                           orderby cert.CardNumber
                           //group cert by cert.CardNumber
                           //into certGrp
                           select cert);

What I need to do is group by CardNumber and do a count of how many orders have that card number.

I would ideally it to come out like a Dictionary if at all possible

2
  • Are we talking about L2EF or L2SQL? If it's EF, see my question about a similar matter: stackoverflow.com/questions/10723439/… Commented Jul 5, 2012 at 19:06
  • My ORM is fluent nHibnerate. Would that still apply? Commented Jul 5, 2012 at 19:10

2 Answers 2

1

If I've correctly understood, for each order you want to list the total number of orders for that card number, in which case the following should work:

        var orders = (from cert in _orderRepository.GetAll()
                      join cert2 in _orderRepository.GetAll()
                      on cert.CardNumber equals cert2.CardNumber
                      where !cert.PrintedPackSlip 
                      where !cert2.PrintedPackSlip
                      group cert by cert
                      into certGrp
                      select certGrp).ToDictionary(o => o.Key,o => o.Count())
Sign up to request clarification or add additional context in comments.

4 Comments

It looks like it isn't possible to get the actual Order out of this, but only the key (or CardNumber)?
Try changing it to .ToDictionary(o => o.First(),o => o.Count()) though that assumes that the first order in a group is representative of all the orders (without seeing the rest of the data which makes up an order I can't say if that is true)
That would not be true. There could be multiple Orders that have the same card number, and we want to group those together to get the Count. Sorry if this is confusing. Thanks for the help though!
OK, I think I know understand what you are trying to do - for each order you want to list the number of orders with the same card number - I've modified by answer to do this
1

May be something like this:

var orders = from cert in _orderRepository.GetAll()
                       where !cert.PrintedPackSlip
                       orderby cert.CardNumber
                       group cert by cert.CardNumber into certGrp
                       select new {CardNumber = certGrp.Key, Count = certGrp.Count());

foreach(var item in orders)
    Console.WriteLine("item.CardNumber: " + item.CardNumber + " item.Count: " + item.Count.ToString());

3 Comments

cert would be out of scope after the grouping, you'd have to use the key. I've made the correction.
This is essentially what I had before, but I would ideally it to come out like a Dictionary<Order, int> if at all possible. (will add to question for clarification.
@mmillican for a dictionary, just wrap this LINQ query in parentheses and append .ToDictionary<Order, int>(x => x.CardNumer, x => x.Count) to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.