-1

可能重复:
linq-to-sql group by 计数和自定义对象模型

我有一个 linq-to-sql 查询,它可以分组和计数,结果最终出现在字典中。我想将字典映射到对象模型的属性。对象模型如下所示:

public class MyCountModel()
{
  int CountSomeByte1 { get; set; }
  int CountSomeByte2 { get; set; }
  int CountSomeByte3 { get; set; }
  int CountSomeByte4 { get; set; }
  int CountSomeByte5 { get; set; }
  int CountSomeByte6 { get; set; }
}

我想映射字典,以便查询像这样结束:

var TheQuery = MyDC.SomeTable
               .Where(...)
               .GroupBy(...)
               .ToDictionary(x => x.Key, x=> x.Count()
               .Select(m => new MyCountModel()
               {
                  CountSomeByte1 = ..., // the value where Key is 1
                  CountSomeByte2 = ...., // the value where Key is 2
                  ....
                  CountSomeByte6 = .... // the value where Key is 6
                });

我怎么能写出这样的东西?感谢您的建议。

4

1 回答 1

2

你不想选择任何东西。我认为您只是想将查询“减少”为单个值:

var dictionary = MyDC.SomeTable
               .Where(...)
               .GroupBy(...)
               .ToDictionary(x => x.Key, x=> x.Count());

var result = new  MyCountModel
               {
                  CountSomeByte1 = dictionary[1],
                  CountSomeByte2 = dictionary[2],
                  ....
                  CountSomeByte6 = dictionary[6]
                });
于 2012-09-28T20:42:24.960 回答