Group By Multiple Columns

Microsoft .NET Framework
Post Reply
User avatar
Phantom8
Posts: 37
Joined: Mon Nov 13, 2017 8:51 pm

Group By Multiple Columns

Post by Phantom8 » Thu Feb 01, 2018 10:04 am

How can I do GroupBy Several Columns in LINQ Something much like this in SQL:

Code: Select all

 CHOOSE * FROM <TableName> GROUP BY <Column1>,<Column2> 
How can I transform this to LINQ:

Code: Select all

 QuantityBreakdown
(
MaterialID int,
ProductID int,
Quantity float
)

PLACE INTO @QuantityBreakdown (MaterialID, ProductID, Amount).
CHOOSE MaterialID, ProductID, AMOUNT( Quantity).
FROM @Transactions.
GROUP BY MaterialID, ProductID 

User avatar
ZoneX
Posts: 38
Joined: Wed Oct 18, 2017 10:38 am

Re: Group By Multiple Columns

Post by ZoneX » Fri Feb 02, 2018 7:47 am

Use a confidential type. Eg

Code: Select all

 group x by brand-new 

User avatar
Maje72
Posts: 1
Joined: Wed Jan 31, 2018 8:42 am

Re: Group By Multiple Columns

Post by Maje72 » Fri Feb 02, 2018 9:18 pm

Procedural sample

Code: Select all

 GroupBy( x => new x.Column1, x.Column2) 

User avatar
Lavey31
Posts: 62
Joined: Fri Sep 15, 2017 3:00 pm

Re: Group By Multiple Columns

Post by Lavey31 » Sat Feb 03, 2018 9:15 am

Ok got this as:

Code: Select all

 var question = (from t in Transactions.
group t by new 
into grp.
choose new.
). ToList(); 

User avatar
Nova6
Posts: 11
Joined: Thu Jan 11, 2018 10:11 am

Re: Group By Multiple Columns

Post by Nova6 » Sun Feb 04, 2018 1:13 pm

You can also use a Tuple<> for a strongly-typed grouping.

Code: Select all

 from grouping in list.GroupBy( x => new Tuple<string,string,string>( x.Person.LastName, x.Person.FirstName, x.Person.MiddleName)).
select brand-new SummaryItem.

LastName = grouping.Key.Item1,.
FirstName = grouping.Key.Item2,.
MiddleName = grouping.Key.Item3,.
DayCount = grouping.Count(),.
AmountBilled = grouping.Sum( x => x.Rate),.

User avatar
JakeBrown
Posts: 51
Joined: Fri Oct 06, 2017 12:51 pm

Re: Group By Multiple Columns

Post by JakeBrown » Sun Feb 04, 2018 1:24 pm

Thought this concern is asking about group by class residential or commercial properties, if you want to group by multiple columns versus a ADO item (like a DataTable), you need to assign your "brand-new" products to variables:

Code: Select all

 EnumerableRowCollection<DataRow> ClientProfiles = CurrentProfiles.AsEnumerable()
. Where( x => CheckProfileTypes.Contains( x.Field<object>( ProfileTypeField). ToString()));.
// do other stuff, then check for dups ...
var Dups = ClientProfiles.AsParallel()
. GroupBy( x => new )
. Where( z => z.Count() > 1)
. Select( z => z); 

User avatar
WallaWalla
Posts: 69
Joined: Fri Sep 08, 2017 9:25 am

Re: Group By Multiple Columns

Post by WallaWalla » Tue Feb 13, 2018 4:48 pm

Since C# 7 you can also utilize value tuples:

Code: Select all

 group x by (x.Column1, x.Column2) 
or

Code: Select all

 GroupBy( x => (x.Column1, x.Column2)) 
Cheers!
WallaWalla

User avatar
RandomGuy
Posts: 49
Joined: Fri Sep 15, 2017 2:14 pm

Re: Group By Multiple Columns

Post by RandomGuy » Wed Feb 14, 2018 6:22 pm

Code: Select all

 var Outcomes= query.GroupBy( f => brand-new * add members here * ); 

Post Reply