可能的重复项:
我怎样才能为此做一个支点
我有一个表格,
如下所示 ID | value | Name
--------+---------------+------------------
1 Digital Model
1 companyA Manufacturer
2 Analog Model
2 CompanyB Manufacturer
3 Fiber Model
3 CompanyC Manufacturer
I need to convert this back to
ID | Model | Manufacturer
--------+--------------+-------------------
1 Digital companyA
2 Analog CompanyB
3 Fiber CompanyC
请帮我解决 Linq 查询。 使用 T-SQL,我能够正确转换它。但是,使用 Linq,我发现编写查询时出现问题。
试试这个:
var results = Data.GroupBy(l => l.Name);
.SelectMany( g =>
new
{
Metadata = g.Key,
data = g
});
var pivoted = new List<PivotedEntity>();
foreach(var item in results)
{
pivoted.Add(
new PivotedEntity
{
Id = item.Id,
Model = item.data.Where(x => x.Name == "Model")
.FirstOrDefault().value,
Manufacturer = item.data.Where(x => x.Name == "Manufacturer")
.FirstOrDefault().value,
});
}
您应该首先定义一个新类:
public class PivotedEntity
{
public int Id {get; set;}
public string Model {get; set;}
public string Manufacturer {get; set;}
}