Я создал структуру данных, аналогичную той, что вы описали в LINQPad. Это код, который у меня есть
void Main()
{
List<Location> locations = new List<Location>
{
new Location { Key = "BLR", Quarter = "Q1", PeopleRequired = 70, PeopleAvailable = 35 },
new Location { Key = "BLR", Quarter = "Q2", PeopleRequired = 50, PeopleAvailable = 45 },
new Location { Key = "BLR", Quarter = "Q3", PeopleRequired = 25, PeopleAvailable = 28 },
new Location { Key = "BLR", Quarter = "Q4", PeopleRequired = 60, PeopleAvailable = 58 },
new Location { Key = "CHN", Quarter = "Q1", PeopleRequired = 77, PeopleAvailable = 92 },
new Location { Key = "CHN", Quarter = "Q2", PeopleRequired = 42, PeopleAvailable = 66 },
new Location { Key = "CHN", Quarter = "Q3", PeopleRequired = 29, PeopleAvailable = 20 },
new Location { Key = "CHN", Quarter = "Q4", PeopleRequired = 22, PeopleAvailable = 24 },
new Location { Key = "CAD", Quarter = "Q1", PeopleRequired = 100, PeopleAvailable = 150 },
new Location { Key = "CAD", Quarter = "Q2", PeopleRequired = 200, PeopleAvailable = 250 },
};
var results =
(
from loc in locations.Select(l => new { l.Key }).Distinct()
join q1 in locations.Where(l => l.Quarter == "Q1") on loc.Key equals q1.Key into quarter1
join q2 in locations.Where(l => l.Quarter == "Q2") on loc.Key equals q2.Key into quarter2
join q3 in locations.Where(l => l.Quarter == "Q3") on loc.Key equals q3.Key into quarter3
join q4 in locations.Where(l => l.Quarter == "Q4") on loc.Key equals q4.Key into quarter4
from q1 in quarter1.DefaultIfEmpty()
from q2 in quarter2.DefaultIfEmpty()
from q3 in quarter3.DefaultIfEmpty()
from q4 in quarter4.DefaultIfEmpty()
select new
{
loc.Key,
Q1_PeopleRequired = q1 != null ? q1.PeopleRequired : -1,
Q1_PeopleAvailable = q1 != null ? q1.PeopleAvailable : -1,
Q2_PeopleRequired = q2 != null ? q2.PeopleRequired : -1,
Q2_PeopleAvailable = q2 != null ? q2.PeopleAvailable : -1,
Q3_PeopleRequired = q3 != null ? q3.PeopleRequired : -1,
Q3_PeopleAvailable = q3 != null ? q3.PeopleAvailable : -1,
Q4_PeopleRequired = q4 != null ? q4.PeopleRequired : -1,
Q4_PeopleAvailable = q4 != null ? q4.PeopleAvailable : -1
}
);
results.Dump();
}
// Define other methods and classes here
public class Location
{
public string Key { get; set; }
public string Quarter { get; set; }
public int PeopleRequired { get; set; }
public int PeopleAvailable { get; set; }
}
В результате я получил именно то, что искал.Это выше может или не может быть ЛУЧШИМ способом, и я делаю лаг на большом столе, но это работает:)
Key | Q1_PeopleRequired | Q1_PeopleAvailable | Q2_PeopleRequired | Q2_PeopleAvailable | Q3_PeopleRequired | Q3_PeopleAvailable | Q4_PeopleRequired | Q4_PeopleAvailable
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
BLR | 70 | 35 | 50 | 45 | 25 | 28 | 60 | 58
CHN | 77 | 92 | 42 | 66 | 29 | 20 | 22 | 24
CAD | 100 | 150 | 200 | 250 | -1 | -1 | -1 | -1