Thursday, May 5, 2011

Exclusionary Set Syntax with Linq, VB

I'd like to do a subtraction of sets based on criteria. A pseudo-query would look like:

select table1.columnn1
      ,table1.column2
  from table1, table2
 where (table1.column1.value1 not in table2.column1
        and
        table1.column2.value2 not in table2.column2)

I can make it to about here:

dim list = From tbl1 In table1 Where tt.column1 ...

And from there I don't know what to do.

From stackoverflow
  • Have a look at the Except standard query operator in LINQ. This produces the set difference of two sequences.

    http://msdn.microsoft.com/en-us/library/system.linq.enumerable.except.aspx

    You might also use the Contains operator to achieve what you want, as per the sample below:

    dim table2Col1 = from t in table2 select t.column1
    dim table2Col2 = from t in table2 select t.column2
    
    dim results = _
       from t in table1 _
       where not table2Col1.Contains(t.column1) _
       and  not table2Col2.Contains(t.column2) _
       select new with { .column1=t.column1, .column2=t.column2 }
    
    hypoxide : Thanks much, you definitely steered me in the right direction.

0 comments:

Post a Comment