LINQ到SQL组通过计数和自定义对象模型

LINQ到SQL组通过计数和自定义对象模型

本文介绍了LINQ到SQL组通过计数和自定义对象模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找,以填补一个LINQ到SQL查询它的密钥组的计数的对象模型。

I'm looking to fill an object model with the count of a linq-to-sql query that groups by its key.

对象模型颇像这样的:

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



ListOfRecordIDs 是在通过多头名单作为参数。所有CountSomeByteN有下划线的红色。你怎么做映射到一个对象模型组的关键分组元素的计数?

ListOfRecordIDs is list of longs that's passed in as a parameter. All the CountSomeByteN are underlined red. How do you do a count of grouped elements with the group's key mapped to an object model?

感谢您的建议。

推荐答案

选择正在你的团队中的每个元素,并将其投射到相同的新创建 MyCountModel s,而你只使用其中的一种。以下是我想做到这一点:

The select is taking each element of your group and projecting them to identical newly created MyCountModels, and you're only using one of them. Here's how I'd do it:

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];
}



编辑:这里是做在一个声明中的一种方式。它使用一个名为进入,基本上可以作为 x.Into(F) == <$ C $扩展方法C> F(X)。在这种情况下,它可以被看作是像选择该对整个枚举,而不是其成员的工作原理。我觉得得心应手消除在这种情况下临时变量,如果我是在一个语句来写这个,它可能是我怎么会做到这一点:

Here's one way to do it in one statement. It uses an extension method called Into, which basically works as x.Into(f) == f(x). In this context, it can be viewed as like a Select that works on the whole enumerable rather than on its members. I find it handy for eliminating temporary variables in this sort of situation, and if I were to write this in one statement, it's probably how I'd do it:

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];
    });

这篇关于LINQ到SQL组通过计数和自定义对象模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:20