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



情况是这样的。

我正在尝试使用 LINQ 在两个查询之间执行Union。我已经使用匿名类型在两个查询中应用了GroupBy。我不确切知道,但匿名类型的使用可能导致我遇到这个问题,我无法执行该联盟操作。它向我显示了这样的异常:

"System.Linq.IQueryable"不包含 "联合"的定义和最佳扩展方法重载 '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 因为记录是重复的。

感谢这里关于我做错了什么的任何建议或指导。

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

我唯一能推测的是日期的。也许第一个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();

最新更新