Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有计数和自定义对象模型的linq到sql分组依据_C#_Linq To Sql - Fatal编程技术网

C# 具有计数和自定义对象模型的linq到sql分组依据

C# 具有计数和自定义对象模型的linq到sql分组依据,c#,linq-to-sql,C#,Linq To Sql,我希望用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; se

我希望用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 = from x in MyDC.TheTable
               where ListOfRecordIDs.Contains(x.RecordID) && x.SomeByte < 7
               group x by x.SomeByte into TheCount
               select new MyCountModel()
               {
                   CountSomeByte1 = TheCount.Where(TheCount => TheCount.Key == 1)
                                            .Select(TheCount).Count(),

                   CountSomeByte2 = TheCount.Where(TheCount => TheCount.Key == 2)
                                            .Select(TheCount).Count(),     
                   .....

                   CountSomeByte6 = TheCount.Where(TheCount => TheCount.Key == 6)
                                            .Select(TheCount).Count(), 

               }.Single();
var TheQuery=MyDC.TheTable中的x
其中,listofRecordId.Contains(x.RecordID)和&x.SomeByte<7
将x按x.SomeByte分组到计数器中
选择新模型()
{
CountSomeByte1=count.Where(count=>count.Key==1)
.选择(Count).Count(),
CountSomeByte2=count.Where(count=>count.Key==2)
.选择(Count).Count(),
.....
CountSomeByte6=count.Where(count=>count.Key==6)
.选择(Count).Count(),
}.Single();
listofRecordds
是作为参数传入的long列表。所有的CountSomeByteN都有红色下划线。当组的键映射到对象模型时,如何对分组元素进行计数


谢谢您的建议。

您的范围变量在子查询中不正确:

     CountSomeByte6 = TheCount.Where(TheCount => TheCount.Key == 6)
                                        .Select(TheCount).Count(), 
在方法表示法中,您不需要额外的选择:

CountSomeByte6 = TheCount.Where(theCount => theCount.Key == 6).Count(), 
如果仍要使用它,请执行以下操作:

CountSomeByte6 = TheCount.Where(theCount => theCount.Key == 6).Select(theCount => theCount).Count(), 

select
将获取组中的每个元素,并将它们投影到新创建的相同的
MyCountModel
s,并且您只使用其中一个。我是这样做的:

var dict = MyDC.TheTable
    .Where(x => ListOfRecordIDs.Contains(x.RecordID) && x.SomeByte < 7)
    .GroupBy(x => x.SomeByte)
    .ToDictionary(grp => grp.Key, grp => grp.Count());

var result = new MyCountModel()
{
    CountSomeByte1 = dict[1];
    CountSomeByte2 = dict[2];
    CountSomeByte3 = dict[3];
    CountSomeByte4 = dict[4];
    CountSomeByte5 = dict[5];
    CountSomeByte6 = dict[6];
}
public static U Into<T, U>(this T self, Func<T, U> func)
{
    return func(self);
}

var result = MyDC.TheTable
    .Where(x => ListOfRecordIDs.Contains(x.RecordID) && x.SomeByte < 7)
    .GroupBy(x => x.SomeByte)
    .ToDictionary(grp => grp.Key, grp => grp.Count())
    .Into(dict => new MyCountModel()
    {
        CountSomeByte1 = dict[1];
        CountSomeByte2 = dict[2];
        CountSomeByte3 = dict[3];
        CountSomeByte4 = dict[4];
        CountSomeByte5 = dict[5];
        CountSomeByte6 = dict[6];
    });

好的,语法错误确实消失了,但是现在,在运行时,我得到一个“Sequence contains more one element”错误。您正在调用Single,这意味着您只需要一个元素,但很明显,正如消息所说,查询返回多个元素。是的,我调用Single是因为我只需要返回一个MyCountModel()。为什么它返回的值超过1??请阅读
Single
的文档。您需要First或FirstOrDefault。事实上,它不起作用:即使它没有崩溃,结果也不准确。我是否还需要orderby来确保CountSomeByte1=dict[1];还有,有没有一种方法可以使它只在一个语句中工作?1)没有。
dict
是一个
字典,而
1
是一个字典键,而不是数组索引
dict[1]
将是
SomeByte
为1的记录计数,因为分组就是这样进行的。2) 我肯定有办法,但我怀疑它是否会如此简单。我来玩玩它。如果你使用它呢。选择字典,然后将它投影到对象模型的属性上?那行吗。选择(x=>newmyCountModel(){…})<代码>选择
用于将一种序列转换为另一种序列。在这种情况下,
字典
被解释为
KeyValuePair
s的
IEnumerable
,但您不想从中获得序列,而是一个对象。因此,
Select
确实不是正确的工具。现在,您可能认为您的
MyCountModel
有点像一个序列,但语言不允许您这样对待它。