Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Linq Pivotting Blues
Message
From
07/04/2017 08:35:41
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
C#
Category:
LINQ
Miscellaneous
Thread ID:
01649939
Message ID:
01649941
Views:
49
This message has been marked as a message which has helped to the initial question of the thread.
>Close, but no cigar.
>
>
>                          using (var context = new PICSEntities(Config.model.SqlServer))
>            {
>                var q = context.TransactionDetails.Where(t => t.Parcel.GoodsType.Description == "Rough")
>                    .Where(t => t.Parcel.ParcelInventoryType.Description == "" +
>                    "Inventory")
>                    .GroupBy(t => new { f1 = t.Parcel.RSReference /*, f2 = t.TransactionHeader.TransactionType.Description */ }, (key, g) =>
>                      new
>                      {
>                          Parcel = key.f1,
>                          BeginningInventory = g.Where(x => x.TransactionHeader.TransactionType.Description == "Beginning Inventory").Select(x => x.Cogs),
>                          Purchase = g.Where(x => x.TransactionHeader.TransactionType.Description == "Purchase").Select(x => x.Cogs),
>                          Transfer = g.Where(x => x.TransactionHeader.TransactionType.Description == "Internal Transfer").Select(x => x.Cogs),
>                          Sale = g.Where(x => x.TransactionHeader.TransactionType.Description == "Sale").Select(x => x.Cogs)
>                      });
>
>
>For the avoidance of doubt, I'm trying to get a table consisting of a line for each Parcel and the total Cogs for each TransactionType (Beginning Inventory, Purchase, Transfer and Sale on the same line but in a different column. It seem to put a collection in each cell where I was hoping to get the Cogs.

Not sure what you mean exactly. Having sample data and output would help. Anyway I hope one of these help:
	using (var context = new PICSEntities(Config.model.SqlServer))
	{
		var q = context.TransactionDetails.Where(t => t.Parcel.GoodsType.Description == "Rough")
			.Where(t => t.Parcel.ParcelInventoryType.Description == "Inventory")
			.GroupBy(t => new { f1 = t.Parcel.RSReference /*, f2 = t.TransactionHeader.TransactionType.Description */ }, (key, g) =>
			  new
			  {
				  Parcel = key.f1,
				  BeginningInventory = g.Where(x => x.TransactionHeader.TransactionType.Description == "Beginning Inventory").Sum(x => x.Cogs),
				  Purchase = g.Where(x => x.TransactionHeader.TransactionType.Description == "Purchase").Sum(x => x.Cogs),
				  Transfer = g.Where(x => x.TransactionHeader.TransactionType.Description == "Internal Transfer").Sum(x => x.Cogs),
				  Sale = g.Where(x => x.TransactionHeader.TransactionType.Description == "Sale").Sum(x => x.Cogs)
			  });
	}


	using (var context = new PICSEntities(Config.model.SqlServer))
	{
		var q = context.TransactionDetails.Where(t => t.Parcel.GoodsType.Description == "Rough")
			.Where(t => t.Parcel.ParcelInventoryType.Description == "Inventory")
			.GroupBy(t => new { f1 = t.Parcel.RSReference /*, f2 = t.TransactionHeader.TransactionType.Description */ }, (key, g) =>
			  new
			  {
				  Parcel = key.f1,
				  BeginningInventory = g.FirstOrDefault(x => x.TransactionHeader.TransactionType.Description == "Beginning Inventory").Cogs,
				  Purchase = g.FirstOrDefault(x => x.TransactionHeader.TransactionType.Description == "Purchase").Cogs,
				  Transfer = g.FirstOrDefault(x => x.TransactionHeader.TransactionType.Description == "Internal Transfer").Cogs,
				  Sale = g.FirstOrDefault(x => x.TransactionHeader.TransactionType.Description == "Sale").Cogs
			  });
	}
PS: If you use LinqPad, you can not only quickly test variations but also copy paste the result easily (link below in my sig).
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform