Solved

LINQ - Group By Question

Posted on 2015-02-10
10
81 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
  • 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

808 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