我是LINQ的新手,但我想知道是否可以使用LINQ来转移以下布局中的数据:
cvMesFicFin cvAnoFicFin cvVlrBasFicFin
08 1998 30
09 1998 30
10 1998 30
11 1998 30
12 1998 30
01 1999 30
02 1999 30
03 1999 30
04 1999 30
05 1999 30
06 1999 30
这样的事情:
Year 01 02 03 04 05 06 07 08 09 10 11 12
1998 NULL NULL NULL NULL NULL NULL NULL 30 30 30 30 30
1999 30 30 30 30 30 30 NULL NULL NULL NULL NULL NULL
答案 0 :(得分:2)
我会用这样的东西:
var r = a.GroupBy(e => e.cvAnoFicFin).Select(g => new
{
Year = g.Key,
Jan = g.Where(e => e.cvMesFicFin == 1)
.Select(c => (int?)c.cvVlrBasFicFin).SingleOrDefault(),
Feb = g.Where(e => e.cvMesFicFin == 2)
.Select(c => (int?)c.cvVlrBasFicFin).SingleOrDefault(),
Mar = g.Where(e => e.cvMesFicFin == 3)
.Select(c => (int?)c.cvVlrBasFicFin).SingleOrDefault(),
Apr = g.Where(e => e.cvMesFicFin == 4)
.Select(c => (int?)c.cvVlrBasFicFin).SingleOrDefault(),
May = g.Where(e => e.cvMesFicFin == 5)
.Select(c => (int?)c.cvVlrBasFicFin).SingleOrDefault(),
Jun = g.Where(e => e.cvMesFicFin == 6)
.Select(c => (int?)c.cvVlrBasFicFin).SingleOrDefault(),
Jul = g.Where(e => e.cvMesFicFin == 7)
.Select(c => (int?)c.cvVlrBasFicFin).SingleOrDefault(),
Aug = g.Where(e => e.cvMesFicFin == 8)
.Select(c => (int?)c.cvVlrBasFicFin).SingleOrDefault(),
Sep = g.Where(e => e.cvMesFicFin == 9)
.Select(c => (int?)c.cvVlrBasFicFin).SingleOrDefault(),
Oct = g.Where(e => e.cvMesFicFin == 10)
.Select(c => (int?)c.cvVlrBasFicFin).SingleOrDefault(),
Nov = g.Where(e => e.cvMesFicFin == 11)
.Select(c => (int?)c.cvVlrBasFicFin).SingleOrDefault(),
Dec = g.Where(e => e.cvMesFicFin == 12)
.Select(c => (int?)c.cvVlrBasFicFin).SingleOrDefault()
});
这个想法是从这个答案中偷来的:https://stackoverflow.com/a/167937/276994。
如果数据表示数据库条目,则cvVlrBasFicFin
可能已经可以为空,因此不需要强制转换。