4

for reporting purposes i wanna split a list of purchase orders into multiple lists. One list for each purchase address. I know it's possible to group the list by purchase address, but my question is how to split the list by this purchase address into multiple lists and use these multiple list to create individual reporting files.

code:

(from o in orders
group o by new {field1, field2, field3, field4} into og
orderby og.Key.field1
select new ViewClass
{
    purchaseAddress = og.Key.field1,
    product = og.key.field2,
    count = og.count
}).ToList()

question: how to split above list into multiple lists for each purchaseAddress?

Ozair Kafray
  • 13,351
  • 8
  • 59
  • 84
Luuk Krijnen
  • 1,180
  • 3
  • 14
  • 37
  • i think your question is more concerned with presentation of data. how it should be plotted on screen. Is it? – Amritpal Singh Jun 09 '12 at 12:24
  • yes it is on presentation but in my case in a reportfile (rdlc). For each purchase address I want to create a specific PDF file. the report file at this moment is create with all addresses on an individual page. This file has to be splitted into multple files which can be e-mailed to each address – Luuk Krijnen Jun 09 '12 at 12:28
  • You are creating single PDF for all data you need to add page break on group field purchase address – Amritpal Singh Jun 09 '12 at 12:30
  • I don't quite understand why you can't use a `group` clause. It will split the list into multiple lists just as you want. – GregRos Jun 09 '12 at 12:33
  • @amritpal-singh: I have pagebreaks. but it stays a single file. if multibple files I can send each address my purchaseorder for them without they have to find out which part is for them. If 4 customers order products, these orders produce multiple internal orders for each producer. – Luuk Krijnen Jun 09 '12 at 12:54
  • @LuukKrijnen you need to iterate for each address and generate seprate pdf file. you can not split like create one pdf and then split by address – Amritpal Singh Jun 09 '12 at 13:22

3 Answers3

11

There's a built-in function that I think does what you want. If I assume that your code is assigned to a variable called query then you can write this:

ILookup<string, ViewClass> queryLists = query.ToLookup(x => x.purchaseAddress);

This essentially creates a list of lists that you access like a dictionary, except that each value is a list of zero or more values. Something like:

IEnumerable<ViewClass> someList = queryLists["Some Address"];
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • 1
    Not knowing how many an which addresses are available i used: foreach (var list in querylists) { dosomething(querylist[list.Key]);} – Luuk Krijnen Jun 10 '12 at 20:37
2

Just turn each group into a List.

select new ViewClass 
{ 
    purchaseAddress = og.Key.field1, 
    product = og.key.field2, 
    count = og.count,
    List = og.ToList()
}).ToList();

Oh, your grouping is one way for entities and another way for pages... just regroup.

List<ViewClass> classes = (
  from o in orders   
  group o by new {field1, field2, field3, field4} into og   
  orderby og.Key.field1   
  select new ViewClass   
  {   
    purchaseAddress = og.Key.field1,   
    product = og.key.field2,   
    count = og.count   
  }).ToList();

List<List<ViewClass>> regrouped = (
  from c in classes
  group c by c.purchaseAddress into g
  select g.ToList()
).ToList();
Amy B
  • 108,202
  • 21
  • 135
  • 185
0

Another simple built-in function that you can use is the GroupBy function. It does a similar job as the ToLookup but it means that your new list is IQuerable, not like a dictionary and a few other things (see this article for a good breakdown)

var newList = orders.GroupBy(x => x.field1);

This will return a list of lists grouped by the field(s) you specify.

Community
  • 1
  • 1
Samuel
  • 811
  • 6
  • 7