c# – LINQ to SQL – 不支持ToDictionary – 如何将结果提取到内存中?

var details= from row in databaseTablewhere row.id equals queryIdselect new {Dict = row.ToDictionary(x = x.Name, x = x.Value),};执行此LINQ to SQL时,我得到错误;System.NotSupportedException: Thequery ...

  var details= from row in databaseTable
    where row.id equals queryId
    select new 
     {
       Dict = row.ToDictionary(x => x.Name, x => x.Value),
     };

执行此LINQ to SQL时,我得到错误;

System.NotSupportedException: The
query operator ‘ToDictionary’ is not
supported.

我需要做的是先将行拉入内存,但我不确定如何.我尝试在各个地方调用ToList,但没有运气.

解决方法:

var fixTotaliserDetails = (from row in databaseTable
                           where row.id == queryId
                           select new 
                           {
                             row.Name,
                             row.Value,
                           })
                          .ToDictionary(x=>x.Name, x=>x.Value);

这会奏效.我认为你可能过度简化了你的答案,因为在linq-to-sql中,表中的项目没有实现IEnumerable< T>

本文标题为:c# – LINQ to SQL – 不支持ToDictionary – 如何将结果提取到内存中?

基础教程推荐