locked
LINQ grouping and looping RRS feed

  • Question

  • User-543160537 posted

    Hi,

    I am trying to loop using LINQ Query, but i am not getting expected result could some one please help..

    DataTable uniquecpemployees = meetingdetails.AsEnumerable()    \\ uniquecpemployees is retriewing 3 rows                         
                                   .GroupBy(r => new { FirstName = r.Field<string>("CS FirstName"), LastName = r.Field<string>("CS LastName") })
                                   .Select(g => g.First())
                                   .CopyToDataTable();
    
    
                                    var UniCptyEmployees = (from DataRow dr in meetingdetails.Rows select dr["FirstName"]).Distinct();  \\ retriews 2 rows 
    
    
                                        foreach (var itememp in UniCptyEmployees)
                                        {
                                            
                                                if (!string.IsNullOrEmpty(uniquecpemployees.Rows[0]["CS FirstName"].ToString()))
                                                {
                                                    
                                                        for (int k = 0; k < uniquecpemployees.Rows.Count; k++)
                                                        {
    
                                                            writer.WriteStartElement("CounterPartyEmployee");
                                                            writer.WriteAttributeString("FirstName", null, uniquecpemployees.Rows[k]["CS FirstName"].ToString());
                                                            writer.WriteAttributeString("LastName", null, uniquecpemployees.Rows[k]["CS LastName"].ToString());                                                       
                                                            writer.WriteFullEndElement();
    
                                                        }
    
                                                }
                                        }
    Event Id Meeting Id FirstName LastName CS FirstName CS LastName
    12345 789 A C ABC ASD
    12345 789 A C XYZ ZXC
    12345 789 A C QWE MNO
    12345 789 A C ABC ASD
    12345 789 A C XYZ ZXC
    12345 789 A C QWE MNO
    12345 789 A C ABC ASD
    12345 789 A C XYZ ZXC
    12345 789 A C QWE MNO
    12345 789 B B ABC ASD
    12345 789 B B XYZ ZXC
    12345 789 B B QWE MNO
    12345 789 B B ABC ASD
    12345 789 B B XYZ ZXC
    12345 789 B B QWE MNO
    12345 789 B B ABC ASD
    12345 789 B B XYZ ZXC
    12345 789 B B QWE MNO

    uniquecpemployees is retriewing 3 rows(ABC,XYZ & QWE) and UniCptyEmployees retriews 2 rows (A & B) so total it is generating 6 rows. But the expected result is 18 rows.

    Also as a note if below is the case it should retriew only one row..

    78956 68283462 S E Sree Vasu
    78956 68283462 S E Sree Vasu

    can some one please help me out..

    Tuesday, October 6, 2015 8:06 PM

Answers

  • User614698185 posted

    Hi sreekanth.jonna,

    But the expected result is 18 rows.

    Based on your LINQ statement, the result is expected. If you need to get 18 row results, I guess you want to groupby FirstName and LastName columns:

    DataTable uniquecpemployees = meetingdetails.AsEnumerable()                      
                                   .GroupBy(r => new { r.FirstName, r.LastName })
                                   .Select(g => g.First())
                                   .CopyToDataTable();

    Then above LINQ statement will get 9 rows as you expected(eg. FirstName: A, LastName: C).

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 7, 2015 9:28 AM