查询中的Linq FirstOrDefault列表

时间:2015-11-15 20:13:53

标签: linq linq-to-entities firebird

我有这个linq查询

var numberGroups =
            from n in VISRUBs.Where(a => a.VISANA.VISITE.DATEVIS <= d && a.VISANA.VISITE.PANUM == p)
            group n by n.RUBRIQUE into g

            select new { 

                RemainderCHAPLIB = g.Key.ANALYSE.CHAPITRE.LIBELLE,
                RemainderLIB = g.Key.LIBELLE,

                RemainderRUNUM = g.Key.RUNUM,
                vals = from vlist in g.OrderByDescending(a=>a.VISANA.VISITE.DATEVIS)
                select vlist.VALEUR
                };

在Linqpad给我这个结果 LinqPadResult

我想要的是从列表的最后一个字段( vals )中选择第一个和第二个项目。

我试过这个:

                var numberGroups =
            from n in VISRUBs.Where(a => a.VISANA.VISITE.DATEVIS <= d && a.VISANA.VISITE.PANUM == p)
            group n by n.RUBRIQUE into g

            select new { 

                RemainderCHAPLIB = g.Key.ANALYSE.CHAPITRE.LIBELLE,
                RemainderLIB = g.Key.LIBELLE,

                RemainderRUNUM = g.Key.RUNUM,
                vals = from vlist in g.OrderByDescending(a =>               a.VISANA.VISITE.DATEVIS)
                select vlist.VALEUR
                };


            var lst = from  n in numberGroups
            select new 
            {
               RemainderCHAPLIB = n.RemainderCHAPLIB,
                RemainderLIB = n.RemainderLIB,

                RemainderRUNUM = n.RemainderRUNUM,
                VAL = n.vals.FirstOrDefault()
            };

但它没有用,我得到了例外

  

动态SQL ErrorSQL错误代码= -104Token unknown - 第54行,第1列

欢迎任何帮助

1 个答案:

答案 0 :(得分:0)

找到它了!

    var lst = from  n in numberGroups.ToList()
            select new 
            {
               RemainderCHAPLIB = n.RemainderCHAPLIB,
                RemainderLIB = n.RemainderLIB,

                RemainderRUNUM = n.RemainderRUNUM,
                VAL = n.vals.FirstOrDefault(),
                ANT = n.vals.Skip(1).FirstOrDefault()
            };