views:

214

answers:

2

I have a few rows of data pulled into business objects via linq-to-sql from large tables.

Now I want to get a few rows that don't match to test my comparison functions.

Using what I thought would work I get a NotSupportedException:

Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator.

Here's the code:

//This table has a 2 field primary key, the other has a single
var AllNonMatches = from c in dc.Acaps
   where !Matches.Rows.Any((row) => row.Key.Key == c.AppId & row.Key.Value == c.SeqNbr)
   select c;

foreach (var item in AllNonMatches.Take(100)) //Exception here
{}

The table has a compound primary key: AppId and SeqNbr.

The Matches.Rows is defined as a dictionary of keyvaluepair(appid,seqnbr).

and the local sequence it is referring to appears to be the local dictionary.

A: 

Could you provide more information on the structure and the name(s) of the table(s) plz?

Not sure what you're trying to do...

edit:

Ok.. I think I get it now...

It appears you can't merge/join local tables (dictionary) with a SQL table. If you can, I'm afraid I don't know how to do it.

The simplest solution I can think of is to put those results in a table ("Match" for instance) with foreign keys related to your table "Acaps" and then use linq-to-sql, like:

var AllNonMatches = dc.Acaps.Where(p=>p.Matchs==null).Take(100).ToList();

Sorry I couldn't come up with any better =(

Francisco
updated the question
Maslow
A: 

What about this:

var AllNonMatches = from c in dc.Acaps
   where !(Matches.Rows.ContainsKey(c.AppId) && Matches.Rows.ContainsValue(c.SeqNbr))
   select c;

That will work fine. I have also used a bitwise AND operator (&&) - I think thats the right term to help improve performance over the standard AND operator.

Robert MacLean
There is a case where this isn't correct though - Imagine we have two relevant items in Matches: (A,B) and (C,D). We're currently selecting an item like (A,D). Though this item doesn't exist in matches (thus it should be selected), it won't be returned as both values exist in Matches (though not on the same item).
Ryan Versaw
This all depends on what the asker needs though - this solution may be sufficient. Also, I doubt ContainsKey() and ContainsValue() will work. You'll likely need to use Keys.Contains() and Values.Contains().
Ryan Versaw