I have problem with Linq, the code used to be processed in sql via stored procedure, but right now it supposed to be on code via linq, here's my database schema
what I want is , from this data
orderNo | Type | serial |
---|---|---|
1 | BN | BN 1 |
1 | BE | BE 1 |
2 | BN | BN 2 |
2 | BE | BE 2 |
3 | BN | BN 3 |
3 | BE | BE 3 |
to be like this :
orderNo | be | bn |
---|---|---|
1 | BE 1 | BN 1 |
2 | BE 2 | BN 3 |
3 | BE 2 | BN 3 |
found one question and solution Source 1 - Stackoverflow , when I tried to my code, I got an issue with SelectMany
here's what I've tried
var results = data_tech.GroupBy(l => l.serial).SelectMany( g =>
new
{
Metadata = g.Key,
data = g
});
var pivoted = new List<PivotedEntity>();
foreach(var item in results)
{
pivoted.Add(
new PivotedEntity
{
Order= item.orderNo,
BE= item.data.Where(x => x.Name == "BE")
.FirstOrDefault().value,
BN= item.data.Where(x => x.Name == "BN")
.FirstOrDefault().value,
});
}