使用LINQ执行GroupBy和Union以获取记录

时间:2015-04-20 11:57:16

标签: c# asp.net-mvc linq group-by union

情况就是这样。

我正在尝试使用LINQ在两个查询之间执行Union。我使用匿名类型在两个查询中都应用了GroupBy。我完全不知道,但是匿名类型的使用可能导致我无法执行该联合操作的问题。它显示我这样的例外:

  

'System.Linq.IQueryable'不包含   'Union'的定义和最佳扩展方法重载   “System.Linq.ParallelEnumerable.Union(System.Linq.ParallelQuery,   System.Collections.Generic.IEnumerable)'有一些无效   参数

以下是查询:

var records = (from o in _context.Table1
                       join q in _context.Table2 on o.UserId equals q.UserId
                       group new { o, q }
                       by new { o.Name, o.Date, o.IsDone, o.IsDl, o.DateChanged, o.UserId, q.FirstName, q.LastName } into data
                       select new
                       {
                           NameP = data.Key.Name,
                           Date = data.Key.Date,
                           IsDone = data.Key.IsDone,
                           IsDl = data.Key.IsDl,
                           DateDone = data.Key.DateChanged,
                           Name = data.Key.FirstName +" "+ data.Key.LastName
                       }).Union(
                            from i in _context.Table1
                            where i.UserId == null
                            group i by new {o.Name, o.Date, o.IsDone, o.IsDl, o.DateChanged, o.UserId} into newData
                            select new
                            {
                                NameP = newData.Key.Name,
                                Date= newData.Key.Date,
                                IsDone = newData.Key.IsDone,
                                IsDl = newData.Key.IsDl,
                                DateDone = ' ',
                                Name = ' '
                            }).ToList();

我在这里想要实现的是获取两种情况的记录,当UserId为空时以及当因为记录被重复而使用group by时它不为空。

对于我所做错的任何建议或指导,我们表示赞赏。

1 个答案:

答案 0 :(得分:1)

确保两个匿名类型具有相同的所有属性的数据类型。

我唯一可以推测的是日期。也许第一个DateDone实际上是DateTime类型,您要将第二个设置为string ......

var records = (from o in _context.Table1
               join q in _context.Table2 on o.UserId equals q.UserId
               group new { o, q }
               by new 
               { 
                   o.Name, 
                   o.Date,  
                   o.IsDone,  
                   o.IsDl,  
                   o.DateChanged,  
                   o.UserId,  
                   q.FirstName,  
                   q.LastName  
               } into data
               select new
               {
                   NameP = data.Key.Name,
                   Date = data.Key.Date,
                   IsDone = data.Key.IsDone,
                   IsDl = data.Key.IsDl,
                   DateDone = data.Key.DateChanged,
                   Name = data.Key.FirstName +" "+ data.Key.LastName
               }).Union(from i in _context.Table1
                        where i.UserId == null
                        group i by new 
                        {
                            o.Name,
                            o.Date, 
                            o.IsDone, 
                            o.IsDl, 
                            o.DateChanged, 
                            o.UserId
                        } into newData
                        select new
                        {
                            NameP = newData.Key.Name,
                            Date = newData.Key.Date,
                            IsDone = newData.Key.IsDone,
                            IsDl = newData.Key.IsDl,
                            DateDone = new DateTime(0),
                            Name = ' '
                        }).ToList();