Solved

LINQ - Group By Question

Posted on 2015-02-10
10
82 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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 Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

696 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