简体   繁体   English

使用LINQ根据是否标记某列来筛选ID

[英]Using LINQ to filter through ID's based on if a certain column is flagged

I have a database that holds InvoiceID and a column boolean flag called Corrected . 我有一个数据库,其中保存InvoiceID和一个名为Corrected InvoiceID标志。 I'm trying to use LINQ to filter whether the database contains these IDs (IDs from another list) but if the Corrected flag is set to true, it would not add that ID. 我正在尝试使用LINQ来筛选数据库是否包含这些ID(来自另一个列表的ID),但是如果Corrected标志设置为true,则不会添加该ID。

This is simple enough but the issue I am having is that there might be the same object ID more than once in my table and if any of them have the Corrected flag I would like to skip them all. 这很简单,但是我遇到的问题是,在我的表中可能存在多个相同的对象ID,并且如果其中任何一个都具有Corrected标志,我想跳过它们。

So far my code only skips the ones that have the flag set to true. 到目前为止,我的代码仅跳过那些将标志设置为true的代码。 So for example, I might have 8 records with the same ID but only 4 of them have the Corrected flag. 因此,例如,我可能有8条具有相同ID的记录,但其中只有4条具有Corrected标志。 Ideally I would skip this ID all together. 理想情况下,我会一起跳过此ID。 The below code will only skip 4 and still include the other 4. 以下代码将仅跳过4,而仍然包括其他4。

var uncorrectedIDs = _context.Table1.SelectMany(y => y.Invoices  
                                    .Where(z => invoiceIDsInPeriod.Contains(z.InvoiceId) 
                       && y.Invoices.Any(w => !w.Corrected))));

How can I adapt this to the above specification? 如何使它适应以上规范? Thanks 谢谢

GroupBy could be one approach GroupBy可能是一种方法

var notCorrected = 
    _context.Table1
            .SelectMany(table => table.Invoices.Where(i => period.Contains(i.InvoiceId))
            .GroupBy(invoice => invoice.Id)
            .Where(group => group.Any(invoice => invoice.Corrected) == false)
            .SelectMany(group => group)
            .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM