Using Dictionary<T1, T2> in LinQ query

I need to execute code which looks like:

Dictionary<Int64, List<Int64>> types;
// initialization of dictionary
results = (from m in d.Linq() 
           where (filter.Types.Any(x =>
                                 x.Key == m.DocumentType.Code
                                 && x.Value.Contains(m.DocumentPurpose.Code)
                                )
                 )
           select m 
          ).ToList();

When I executed this test, I received System.NullReferenceException. But I'm sure that object types isn't null and contains at least one pair (Key: 26; Value: 2, 4).

I think that LINQ can't convert this Any() expression to SQL. How i can rewrite this query?

Upvotes: 0

Views: 166

Answers (1)

Joe
Joe

Reputation: 82614

try this:

results = (from m in d.Linq() 
           where (m.DocumentType != null &&
                  m.DocumentPurpose != null &&
                  filter.Types.Any(x =>
                                 x.Key == m.DocumentType.Code
                                 && x.Value.Contains(m.DocumentPurpose.Code)
                                )

Upvotes: 2

Related Questions