Solved

LINQ - Group By Question

Posted on 2015-02-10
10
80 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 62

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 62

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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 62

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 62

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 62

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

770 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