简体   繁体   中英

LINQ query where complete group meets a condition

I have stored a lot of text lines like this:

1|1000|1|0|Text Message|||
1|1000|1|1|Text Message|||
1|1000|2|0|Text Message|||
1|1000|2|1|Text Message|||
1|1000|3|0|Text Message|||
1|1001|1|0|Text Message|||

in a Collection: List<ObjRow> listRows

and this is the corresponding Class:

public class ObjRow
{
    private string n_Par { get; set; }
    private string n_Rad { get; set; }
    private string n_Lang { get; set; }
    private string n_Line_Mex { get; set; }
    private string text_Mex { get; set; } 
    private int n_Row { get; set; }
}

I would like to find which groups of lines (grouped by property n_Rad , 2° PIPE value) which have not the value n_Lang == 3 (3° PIPE value).

How to do this with LINQ?

This should be what you want:

var groupsWithoutLang3  = listRows
             .GroupBy(o => o.n_Rad)
             .Where(g => !g.Any(o => o.n_Lang == "3"));

It selects only groups without an ObjRow with n_Lang == "3" .

var groups = listRows.GroupBy(row => row.Rad);
var result = groups.Where(group => !group.Any(item => (item.Lang == 3)));

This groups the rows by Rad and then selects the groups that do not contain a row whose Lang is 3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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