创建字典时Linq-to-SQL编译器错误

时间:2011-10-28 01:42:57

标签: c# linq-to-sql windows-phone-7 dictionary

我正在尝试开发一个简单的WP7数据库应用程序,该应用程序使用Linq2SQL查询数据库,但今天我设法收到内部编译器错误:(

我的查询的简化版本如下(Results是一个保存搜索结果数据的类):

var query =
(
  from x in MyTable
  join y in MyOtherTable on x.Key equals y.Key
  where SqlMethods.Like(x.Field, "%" + searchTerm + "%")
  select new Results
  {
    MyResultField = new Dictionary<MyEnum,string>()
    {
      { MyEnum.Value, x.Field },
      { MyEnum.OtherValue, y.Field }
    },
    ...
  }
);

但是,尝试创建这样的新Dictionary会导致大量编译器错误,即使IntelliSense不反对。我一直在搜索,但我不确定如何在不尝试创建Dictionary的情况下实现类似的结果。

对于一个绝望的新手的任何想法?!非常感谢提前!

更新

抱歉,我忘了包含编译器错误。除了#6,他们对我来说是胡言乱语,但如果它有帮助......

# Error 1   Internal Compiler Error: stage 'BEGIN'  MyProject
# Error 2   Internal Compiler Error: stage 'EMIT'   MyProject
# Error 3   Internal Compiler Error: stage 'COMPILE'    MyProject
# Error 4   Internal Compiler Error: stage 'COMPILE'    C:\...\ViewModel.cs MyProject
# Error 5   Internal Compiler Error: stage 'COMPILE' symbol '<global namespace>'    C:\...\ViewModel.cs MyProject
# Error 6   Internal Compiler Error (0xc0000005 at address 681AB648): likely culprit is 'BIND'.
# An internal error has occurred in the compiler. To work around this problem, try simplifying or changing the program near the locations listed below. Locations at the top of the list are closer to the point at which the internal error occurred. Errors such as this can be reported to Microsoft by using the /errorreport option.
#   MyProject
# Error 7   Internal Compiler Error: stage 'COMPILE' symbol 'ChineseClassmate'  C:\...\ViewModel.cs 12  11  MyProject
# Error 8   Internal Compiler Error: stage 'COMPILE' symbol 'ChineseClassmate.MyProject'    C:\...\ViewModel.cs 12  11  MyProject
# Error 9   Internal Compiler Error: stage 'COMPILE' symbol 'ChineseClassmate.MyProject.ViewModel'  C:\...\ViewModel.cs 12  11  MyProject
# Error 10  Internal Compiler Error: stage 'COMPILE' symbol 'MyProject.ViewModel.MyProjectViewModel'    C:\...\ViewModel.cs 17  15  MyProject
# Error 11  Internal Compiler Error: stage 'COMPILE' symbol 'MyProject.ViewModel.MyProjectViewModel.SearchDatabase(string)' C:\...\ViewModel.cs 57  15  MyProject
# Error 12  Internal Compiler Error: stage 'COMPILE' symbol 'MyProject.ViewModel.MyProjectViewModel.SearchDatabase(string)' C:\...\ViewModel.cs 57  15  MyProject
# Error 13  Internal Compiler Error: stage 'COMPILE' symbol 'MyProject.ViewModel.MyProjectViewModel.SearchDatabase(string)' C:\...\ViewModel.cs 57  15  MyProject
# Error 14  Internal Compiler Error: stage 'BIND' symbol 'MyProject.ViewModel.MyProjectViewModel.SearchDatabase(string)'    C:\...\ViewModel.cs 57  15  MyProject

2 个答案:

答案 0 :(得分:1)

我不相信你能做到这一点。它必须能够将此调用转换为有效的sql并在其中使用新对象,因此我相信会导致失败,因为它无法转换为在db端运行all。您必须得到结果,然后转换为字典(.ToDictionary())

答案 1 :(得分:1)

为每一行创建一个字典并让每个字典只有相同的两个键有点奇怪。最好将Results修改为只有两个字段,如下所示:

var query =
    from x in MyTable
    join y in MyOtherTable on x.Key equals y.Key
    where SqlMethods.Like(x.Field, "%" + searchTerm + "%")
    select new Results
    {
        MyEnumValue = x.Field,
        MyEnumOtherValue = y.Field
        ...
    };

如果您迫切需要在那里使用字典,那么可能会以匿名类型返回值,然后,在强制使用.ToArray()之类的内容执行查询后,您可以创建Results个对象。现有的字典代码。

我希望这会有所帮助。