使用 LINQ 执行 GroupBy 和 Union 来获取记录
Performing GroupBy and Union using LINQ to fetch records
情况是这样的。
我正在尝试使用 LINQ 在两个查询之间执行 Union
。我在两个查询中都应用了 GroupBy
,使用的是匿名类型。我不太清楚,但可能是匿名类型的使用导致我无法执行该 Union 操作的问题。它向我显示了这样的异常:
'System.Linq.IQueryable' does not contain a
definition for 'Union' and the best extension method overload
'System.Linq.ParallelEnumerable.Union(System.Linq.ParallelQuery,
System.Collections.Generic.IEnumerable)' has some invalid
arguments
这里是查询:
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
为 null 时以及当它不为 null 时使用 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();
情况是这样的。
我正在尝试使用 LINQ 在两个查询之间执行 Union
。我在两个查询中都应用了 GroupBy
,使用的是匿名类型。我不太清楚,但可能是匿名类型的使用导致我无法执行该 Union 操作的问题。它向我显示了这样的异常:
'System.Linq.IQueryable' does not contain a definition for 'Union' and the best extension method overload 'System.Linq.ParallelEnumerable.Union(System.Linq.ParallelQuery, System.Collections.Generic.IEnumerable)' has some invalid arguments
这里是查询:
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
为 null 时以及当它不为 null 时使用 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();