在LinQ查询中使用Dictionary <t1,t2 =“”> </t1,>

时间:2012-08-08 05:29:36

标签: c# sql linq

我需要执行看起来像的代码:

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();

当我执行此测试时,我收到了System.NullReferenceException。但我确信对象types不是null并且至少包含一对(Key:26; Value:2,4)。

我认为LINQ无法将此Any()表达式转换为SQL。我如何重写这个查询?

1 个答案:

答案 0 :(得分:2)

试试这个:

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)
                                )
相关问题