0
public class RegistryTableItem
{      
  public int Root { get; set; }//assume possible values are 0,1,2,3      
  public string Name { get; set; }     
  public string Component_ { get; set; }
 }

List<RegistryTableItem> _all_RegistryTable_items = //assume you have data in this;

 var result= from regitems in MsiReader._all_RegistryTable_items
             group regitems by regitems.Component_;
             having (root==1 || root==3) && (root==0 || root==2);//fix this line

The rule is rootcolumn values in each Componentgroup should be 0,2 or 1,3 (Only these combinations are valid groups). I want items which do not pass this rule.

eg: if a component_x has RegistryTableItems with distinct root values as 1,0 or 2,3 or 1,2,3 etc it should come in the result since they are not part of the valid 2 groups.


Try this:

var r = from regitems in MsiReader._all_RegistryTable_items
        group regitems by regitems.Component_ into groupedItems
        where !(groupedItems.Select(i => i.Root).Distinct()
            .OrderBy(i => i).SequenceEqual(new[] { 1, 3 })
        || groupedItems.Select(i => i.Root).Distinct()
            .OrderBy(i => i).SequenceEqual(new[] { 0, 2 }))
        select groupedItems;

Note: There is no direct having clause in Linq, you should use where instead.

4

1 回答 1

1

尝试这个:

var r = from regitems in MsiReader._all_RegistryTable_items
        group regitems by regitems.Component_ into groupedItems
        where !(groupedItems.Select(i => i.Root).Distinct()
            .OrderBy(i => i).SequenceEqual(new[] { 1, 3 })
        || groupedItems.Select(i => i.Root).Distinct()
            .OrderBy(i => i).SequenceEqual(new[] { 0, 2 }))
        select groupedItems;

注意:Linq 中没有直接having子句,应该where改用。

于 2013-05-03T14:27:09.503 回答