Solved

LINQ - Group By Question

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

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now