Solved

LINQ - Group By Question

Posted on 2015-02-10
10
83 Views
Last Modified: 2015-02-13
Hello all,

So I have a List that what I need to do is GroupBy a certain field then take each group and iterate through that group.   What I have is a List<MyViewModel> and it has a field called DataSourceID.   I need to GroupBy this field and then for each group I need to call an Insert statement to insert a audit record then I will get a AuditID back and then I need to iterate through that Group to create records and set that AuditID to each of those records.   For example:

DataSourceID      CustomerName      CustomerLocation
1                            ABC                           Arizona
2                            DEF                           California
1                            GHI                           Nevada
1                            JKL                             Massachusetts
2                            MNO                         New Hampshire

So I would want to get two sets to loop through and for each set create a single insert statement then loop through that group.

Hope this makes sense.
Thanks for any help.
0
Comment
Question by:sbornstein2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40601435
Hi sbornstein2;

Try the below query, it will do what you want.

// Test data      
List<MyViewModel> mylist = new List<MyViewModel>() {
    new MyViewModel() { DataSourceID = 1, CustomerName = "ABC", CustomerLocation = "Arizona" },
    new MyViewModel() { DataSourceID = 2, CustomerName = "DEF", CustomerLocation = "California" },
    new MyViewModel() { DataSourceID = 1, CustomerName = "GHI", CustomerLocation = "Nevada" },
    new MyViewModel() { DataSourceID = 1, CustomerName = "JKL", CustomerLocation = "Massachusetts" },
    new MyViewModel() { DataSourceID = 2, CustomerName = "MNO", CustomerLocation = "New Hampshire" }
};

// Linq query
var results = from mvm in mylist
              group mvm by mvm.DataSourceID into myGroup
              select myGroup;

// Iterate through each group
foreach (var grouping in results)
{
    // Iterate through each record in each group
    foreach (var recordinGroup in grouping)
    {
        // create records and set that AuditID to each of those records
        // ....
        Console.WriteLine("{0}\t{1}\t{2}", recordinGroup.DataSourceID, recordinGroup.CustomerName, recordinGroup.CustomerLocation);
    }
}

// Test Class
public class MyViewModel                        
{                                               
    public int DataSourceID { get; set; }       
    public string CustomerName { get; set; }    
    public string CustomerLocation { get; set; }
}    

Open in new window

0
 

Author Comment

by:sbornstein2
ID: 40601531
for some reason I am not seeing the properties come up in the foreach loop such as grouping.DataSourceID its just showing the aggregate properties like All, Select etc.  maybe missing a using statement?
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40601547
Try changing this line of the code

foreach (var recordinGroup in grouping)

to this

foreach (MyViewModel recordinGroup in grouping)
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:sbornstein2
ID: 40601604
Unable to cast object of type 'Grouping[System.Int32,ClientBilling.Web.UI.Common.ViewModels.PortfolioMarketValueEntryViewModel]' to type 'ClientBilling.Web.UI.Common.ViewModels.PortfolioMarketValueEntryViewModel'.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40601618
Lets start with posting your implementation of the query I posted and the classes involved with the query.
0
 

Author Comment

by:sbornstein2
ID: 40601649
Its not enumerating.  I can get the DataSourceID in the first loop with the .Key.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40601661
In order to figure out what is happening I need to see how you implemented the query I posted and the classes that it is working with.
0
 
LVL 11

Expert Comment

by:louisfr
ID: 40603568
for some reason I am not seeing the properties come up in the foreach loop such as grouping.DataSourceID
Fernando's code contains recordinGroup.DataSourceID, not grouping.DataSourceID. Maybe you didn't copied it correctly.
0
 

Author Closing Comment

by:sbornstein2
ID: 40607854
thank you I figured it with your query
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40607863
Not a problem sbornstein2, glad I was able to help.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question