[英]LINQ how to select ID that exists in one list but not exist in the other
I have two models like this:我有两个这样的模型:
public class Process
{
public int Id { get; set; }
public int PaletID { get; set; }
public Palet Palet { get; set; }
public DateTime Start { get; set; }
public DateTime? End { get; set; }
}
public class Palet
{
public int Id { get; set; }
public int DeliveryNoteID { get; set; }
public DateTime Date { get; set; }
public string Type { get; set; }
public int Length { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public int? Nrpcs { get; set; }
public double Volume { get; private set; }
public Status Status { get; set; }
public DeliveryNote DeliveryNote { get; set; }
}
What I need to do is select (for autocomplete), Ids that exist in Palet, but do not exist in Process and that begin with the keyword.我需要做的是 select(用于自动完成),Palet 中存在但 Process 中不存在且以关键字开头的 ID。
I would like to access data in this way:我想以这种方式访问数据:
string keyword;
var Data = await _context.Process
.Include(p => p.Palet)
.OrderBy(p => p.PaletID)
.Take(dm.Take)
.ToListAsync();
The return should be an int or even better object Processes How can I do this?返回值应该是 int 或更好 object Processes 我该怎么做?
What I need to do is select (for autocomplete), Ids that exist in Palet, but do not exist in Process.我需要做的是 select(用于自动完成),Palet 中存在但 Process 中不存在的 ID。
listOfPalet.Select(p => p.Id).Except(listOfProcess.Select(p => p.Id))
Note that the operation will also deduplicate the list of Palet IDs.请注意,该操作还将对 Palet ID 列表进行重复数据删除。 The output is a list of just the IDs. output 只是 ID 的列表。 If you want the whole Palet object, let me know and I'll make a revision如果你想要整个 Palet object,请告诉我,我会进行修改
Sounds like a job for .GroupJoin()
.听起来像是.GroupJoin()
的工作。 Maybe something like this:也许是这样的:
IReadOnlyList<Palet> palets = LoadPalets();
IReadOnlyList<Process> processes = LoadProcesses();
var paletsWithoutProcess = palets
.GroupJoin(
processes,
palet => palet.Id,
process => process.PaletId,
(palet, processes) => processes.Any() ? null : palet)
.Where(palet => palet != null)
.ToList();
thank you for your hints.谢谢你的提示。 The IEnumerable.Except()
did the trick. IEnumerable.Except()
成功了。 The final solution look like that:最终解决方案如下所示:
var Data = await _context.Paleta
.Select(p => p.Id)
.Except(_context.Process.Select(p => p.PaletaID))
.Where(p => p.ToString().Contains(keyword))
.OrderBy(i=>i)
.Take(20)
.ToListAsync();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.