views:

631

answers:

1

Hi guys,

im trying my best to learn LINQ but im still having a hard time coding it. Like this one, suppose I have a dataset or a List maybe and the names or fields of the of the collection object are the column names.

Id | Date |Mon |Tues |Wed |Thu |Fri |Sat |Sun |Count

1 | 01/05 |=1=|==1==|==1=|==1=| 1=|=0=|=0==|==5 <-- (1)

2 | 02/02 |=1=|==1==|==1=|==1=| 1=|=0=|=0==|==5 **|-- (2)

3 | 03/02 |=1=|==1==|==1=|==1=| 1=|=0=|=0==|==5 **|-- (2)

4 | 04/06 |=1=|==1==|==1=|==1=| 1=|=1=|=1==|==7 <-- (1)

5 | 05/04 |=1=|==1==|==1=|==1=| 1=|=1=|=1==|==7 **|-- (3)

6 | 06/01 |=1=|==1==|==1=|==1=| 1=|=1=|=1==|==7 **|-- (3)

7 | 07/06 |=1=|==1==|==1=|==1=| 0=|=0=|=0==|==4 <---- (1)

8 | 08/03 |=1=|==1==|==1=|==1=| 0=|=0=|=0==|==4 **|-- (4)

9 | 09/07 |=1=|==1==|==1=|==1=| 0=|=0=|=0==|==4 **|-- (4)

10 | 10/05 |1=|==1==|==1=|==1=| 0=|=0=|=0==|==4 **|-- (4)

All i want is first to get all the number (1)'s then the (2)'s because they belong to the first (1). Next the group of (3)'s because they belong to the second (1). Last the group of (4)'s because they belong to the last (1).

please help.

-- question rephrased. 1. how can i get the first group of 5's then the group of 7's and last the group of 4's?

+4  A: 

It appears you want to want to order by count.

When you say "get the first group of 5's" what do you mean - what data do you want to get?

UPDATE after clarification

Assuming

public class Row
{
    public int ID{get;set;}
    public string Date{get;set;}
    public int Count{get;set;}
}

Row r1 = new Row{ID=1, Date="01/01/01", Count=5};
Row r2 = new Row{ID=2, Date="01/02/01", Count=5};
Row r3 = new Row{ID=3, Date="01/03/01", Count=5};
Row r4 = new Row{ID=4, Date="01/04/01", Count=7};
Row r5 = new Row{ID=5, Date="01/05/01", Count=7};
Row r6 = new Row{ID=6, Date="01/06/01", Count=7};
Row r7 = new Row{ID=7, Date="01/07/01", Count=4};
Row r8 = new Row{ID=8, Date="01/08/01", Count=4};
Row r9 = new Row{ID=9, Date="01/09/01", Count=4};
Row r10 = new Row{ID=10, Date="01/01/01", Count=4};

List<Row> rows = new List<Row>{r1,r2,r3,r4,r5,r6,r7,r8,r9,r10};

Then

// We will assign results of our query to this variable
var result = 

// rows is a generic list of Row objects
rows                  

   // This splits the list into seperate categories organised by Count
   // After the GroupBy, we have an IEnumerable<IGrouping<Int32, Row>> - that is, a collection of collections of items sharing a common key (in this case Count)
   .GroupBy(r=>r.Count)  // r is of type Row

    // Now we are simply selecting the first item of each subgroup.
   .Select(g=>g.First()) // g is IGrouping<Int32,Row>, g.First() is of type Row
   ;

Gives

   ID    Date        Count
    1    01/01/01    5
    4    01/04/01    7
    7    01/07/01    4
Winston Smith
@joe, the whole row, if its a dataset.
grayman
Or the whole object if its a member of a List<T>
grayman
Sorry I still don't understand. Since there are multiple rows with the same count, what do you mean the whole row? Can you add an example of the output you'd expect?
Winston Smith
perhaps something like this, i want to get the id, the date and the count of the first row of the group, let's say the group of 5's.Then I'd like to do the same with the group of 7's, I want to obtain the id, date, and the count of the first row of the group of 7's.
grayman
What constitutes the "first" row? First by datetime?
neouser99
Since it wasn't specified, the query above will return the first record in each group according to the current order of the list. Of course, if a specific ordering is required, the query could be adapted to include that.
Winston Smith
thanks so much joe! you got what i needed!! since im quite new with LINQ could you help me as well by explaining you LINQ code? like what 'Xaisoft' posted here.. http://stackoverflow.com/questions/808119/explain-this-linq-code
grayman
Explanation added as requested
Winston Smith