使用 group by 子句时,如何在 Linq 查询中进行嵌套投影?

发布于 2024-12-01 04:05:20 字数 13780 浏览 0 评论 0 原文

我正在尝试处理从 SQL 返回的分组数据。 我正在编写的方法是为“案例状态概述”屏幕提供数据。 它必须生成一个嵌套的 XML 文档。

现在,我可以用简单的方法做到这一点,但我正在尝试了解是否可以使用 linq“group by”语句,然后投影已经嵌套的数据。 (最简单的方法就是从数据库中以表格形式提取数据,然后对其进行 for 循环,形成用于输出的 Xml 文档)

以下是数据层次结构:

每个 Case 都有一个 DebtType,每个 DebtType 都有一个 Client 。

下面是检索数据的 SQL:

SELECT   ClientNames.ClientID                                             ,
         ClientNames.ClientCode                                           ,
         ClientNames.ClientName                                           ,
         DebtTypes.DebtTypeID                                             ,
         DebtTypes.DebtTypeShortDesc                                      ,
         DebtTypes.DebtTypeLongDesc                                       ,
         Cases.CurrentStateCode                                           ,
         SUM(1 - CAST(Cases.CaseClosed AS INT))  AS OpenCaseCount         ,
         SUM(CAST(Cases.CaseClosed AS     INT))  AS ClosedCaseCount       ,
         SUM(CAST(Cases.CaseOnHold AS     INT))  AS OnHoldCaseCount       ,
         SUM(CAST(Cases.CaseReferred AS   INT))  AS ReferredCaseCount     ,
         COUNT(Cases.CaseID)                     AS TotalCaseCount        ,
         SUM(Cases.CaseTotalPaid)                AS TotalAmountPaid       ,
         SUM(Cases.CaseCurrentOutstandingAmount) AS TotalAmountOutstanding,
         SUM(Cases.CaseTotalDebtWrittenOff)      AS TotalAmountWrittenOff ,
         SUM(Cases.CaseTotalDebtCancelled)       AS TotalAmountCancelled
FROM     ClientNames
         INNER JOIN ClientDebtTypes
         ON       ClientNames.ClientID = ClientDebtTypes.ClientID
         INNER JOIN DebtTypes
         ON       ClientDebtTypes.DebtTypeID = DebtTypes.DebtTypeID
         INNER JOIN Cases
         ON       ClientDebtTypes.ClientDebtTypeID = Cases.CaseClientDebtTypeID
GROUP BY ClientNames.ClientID       ,
         ClientNames.ClientCode     ,
         ClientNames.ClientName     ,
         DebtTypes.DebtTypeID       ,
         DebtTypes.DebtTypeShortDesc,
         DebtTypes.DebtTypeLongDesc ,
         Cases.CurrentStateCode
ORDER BY ClientNames.ClientID,
         DebtTypes.DebtTypeID,
         CurrentStateCode

使用 Linqer 将其转换为:

from clientnames in db.ClientNames
join clientdebttypes in db.ClientDebtTypes on clientnames.ClientID equals clientdebttypes.ClientID
join debttypes in db.DebtTypes on clientdebttypes.DebtTypeID equals debttypes.DebtTypeID
join cases in db.Cases on new { ClientDebtTypeID = clientdebttypes.ClientDebtTypeID } equals new { ClientDebtTypeID = cases.CaseClientDebtTypeID }
group new {clientnames, debttypes, cases} by new {
  clientnames.ClientID,
  clientnames.ClientCode,
  clientnames.ClientName1,
  debttypes.DebtTypeID,
  debttypes.DebtTypeShortDesc,
  debttypes.DebtTypeLongDesc,
  cases.CurrentStateCode
} into g
orderby
  g.Key.ClientID,
  g.Key.DebtTypeID,
  g.Key.CurrentStateCode
select new {
  ClientID = (System.Int32?)g.Key.ClientID,
  g.Key.ClientCode,
  g.Key.ClientName1,
  DebtTypeID = (System.Int32?)g.Key.DebtTypeID,
  g.Key.DebtTypeShortDesc,
  g.Key.DebtTypeLongDesc,
  g.Key.CurrentStateCode,
  OpenCaseCount = (System.Int64?)g.Sum(p => 1 - Convert.ToInt32(p.cases.CaseClosed)),
  ClosedCaseCount = (Int32?)g.Sum(p => Convert.ToInt32(p.cases.CaseClosed)),
  OnHoldCaseCount = (Int32?)g.Sum(p => Convert.ToInt32(p.cases.CaseOnHold)),
  ReferredCaseCount = (Int32?)g.Sum(p => Convert.ToInt32(p.cases.CaseReferred)),
  TotalCaseCount = (Int64?)g.Count(p => p.cases.CaseID != null),
  TotalAmountPaid = (System.Decimal?)g.Sum(p => p.cases.CaseTotalPaid),
  TotalAmountOutstanding = (System.Decimal?)g.Sum(p => p.cases.CaseCurrentOutstandingAmount),
  TotalAmountWrittenOff = (System.Decimal?)g.Sum(p => p.cases.CaseTotalDebtWrittenOff),
  TotalAmountCancelled = (System.Decimal?)g.Sum(p => p.cases.CaseTotalDebtCancelled)
}

现在正如我提到的,我可以停在那里并使用 for 循环来创建 Xml 数据。 但我正在尝试创建一个嵌套组 (IGrouping>) 然后以嵌套格式投影数据。

现在我们使用 LinqToXsd 为 out Xml 文档创建强类型包装器,但本质上这意味着 out 输出类型是:

private class ClientSummary
{
    public string ClientName { get; set; }
    public IList<DebtTypeSummary> DebtTypes { get; set; }
}

private class DebtTypeSummary
{
    public string DebtType { get; set; }
    public IList<StateCodeSummary> StateCodes { get; set; }
}

private class StateCodeSummary
{
    public string StateCode { get; set; }
    public int TotalCount { get; set; }
    public decimal TotalAmountPaid { get; set; }
    //etc
    //etc
    //etc
}

现在我编写了以下 Linq:

var grouping = from cases in db.Cases
              join clientdebttypes in db.ClientDebtTypes on cases.CaseClientDebtTypeID equals clientdebttypes.ClientID
              join debttypes in db.DebtTypes on clientdebttypes.DebtTypeID equals debttypes.DebtTypeID
              group cases by new ClientDebtTypePair() { ClientDebtType = clientdebttypes, DebtType = debttypes } into casesByClientDebtTypes
              join clientnames in db.ClientNames on casesByClientDebtTypes.Key.ClientDebtType.ClientName equals clientnames
              group casesByClientDebtTypes by clientnames;

var projected = from casesByClientDebtTypes in grouping
            let client = casesByClientDebtTypes.Key
            select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType()
              {
                  Client = new Client()
                  {
                      ClientID = client.ClientID,
                      DisplayName = client.ClientName1,
                  },
                  DebtTypes = from cases in casesByClientDebtTypes
                              let debttype = cases.Key.DebtType
                              select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType.DebtTypesLocalType()
                              {
                                   DebtType = new DebtType()
                                   {
                                        DebtTypeID = debttype.DebtTypeID,
                                         Description = debttype.DebtTypeLongDesc,
                                          DisplayName = debttype.DebtTypeShortDesc,
                                   },
                                    StatesCodes = from cases2 in cases
                                                  select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType.DebtTypesLocalType.StatesCodesLocalType()
                                                  {
                                                       ClosedCasesCount = cases2.Sum(p => Convert.ToInt32(p.cases.CaseClosed))

它连接并分组数据库表,然后尝试将结果投影为 ClientSummary(类名称不同,但这是因为上面是输出类的简化视图)。当我一直深入到 Cases 表时,我完全失败了,我发现我并不真正理解如何执行聚合函数。它们似乎只在 IGrouping 上可用,看来我只是感到困惑。

我还需要确保摘要是在服务器端计算的,撤回数百万个案例会很糟糕。

有人可以帮我解决这个问题吗?这可能吗?

问候,

詹姆斯。

--------### UPDATE 1 ###-------

好的,今天又开始研究这个问题了。 我决定使用 Linq2SQL 来提取包 2D 数据,然后使用 Linq2Objects 重新格式化它。

这是我开始的内容:

var sql = from clientnames in db.ClientNames
      join clientdebttypes in db.ClientDebtTypes on clientnames.ClientID equals clientdebttypes.ClientID
      join debttypes in db.DebtTypes on clientdebttypes.DebtTypeID equals debttypes.DebtTypeID
      join cases in db.Cases on new { ClientDebtTypeID = clientdebttypes.ClientDebtTypeID } equals new { ClientDebtTypeID = cases.CaseClientDebtTypeID }
      group new { clientnames, debttypes, cases } by new
      {
          clientnames.ClientID,
          clientnames.ClientCode,
          clientnames.ClientName1,
          debttypes.DebtTypeID,
          debttypes.DebtTypeShortDesc,
          debttypes.DebtTypeLongDesc,
          cases.CurrentStateCode
      } into g
      orderby
        g.Key.ClientID,
        g.Key.DebtTypeID,
        g.Key.CurrentStateCode
      select new
      {
          Client = new Client{ ClientID = g.Key.ClientID, DisplayName = g.Key.ClientName1 },
          DebtType = new DebtType{ DebtTypeID = g.Key.DebtTypeID, DisplayName = g.Key.DebtTypeShortDesc, Description = g.Key.DebtTypeLongDesc },
          StateSummary = new LoadCaseStatusOverviewScreenOutput.ClientsLocalType.DebtTypesLocalType.StatesCodesLocalType()
          {
              StateCode = g.Key.CurrentStateCode,
              OpenCasesCount = g.Sum(p => 1 - Convert.ToInt32(p.cases.CaseClosed)),
              ClosedCasesCount = g.Sum(p => Convert.ToInt32(p.cases.CaseClosed)),
              OnHoldCasesCount = g.Sum(p => Convert.ToInt32(p.cases.CaseOnHold)),
              ReferredCasesCount = g.Sum(p => Convert.ToInt32(p.cases.CaseReferred)),
              TotalCasesCount = g.Count(p => p.cases.CaseID != null),
              TotalAmountPaid = g.Sum(p => p.cases.CaseTotalPaid),
              TotalAmountOutstanding = g.Sum(p => p.cases.CaseCurrentOutstandingAmount),
              TotalAmountWrittenOff = g.Sum(p => p.cases.CaseTotalDebtWrittenOff),
              TotalAmountCancelled = g.Sum(p => p.cases.CaseTotalDebtCancelled),
          }
      };
var res = sql.ToList();

output.Clients = (from results in res
              group results by results.Client into resultsByClient
              from resultsByDebtType in
                  (from results in resultsByClient
                   group results by results.DebtType)
              group resultsByDebtType by resultsByClient.Key into resultsByDebtTypeByClient
              select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType()
              {
                  Client = resultsByDebtTypeByClient.Key,
                  DebtTypes = (from resultsByDebtType in resultsByDebtTypeByClient
                               select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType.DebtTypesLocalType()
                               {
                                   DebtType = resultsByDebtType.Key,
                                   StatesCodes = (from results in resultsByDebtType
                                                  let summary = results.StateSummary
                                                  select results.StateSummary).ToList()
                               }).ToList()
              }).ToList();

它运行,但为每个结果生成一个客户/债务类型/摘要集。因此,尽管本例中只有一个客户端,但我最终会得到 1300 个客户端,而且全部相同。 我将其简化为:

output.Clients = (from results in res
             group results by results.Client into resultsByClient
             select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType()
             {
                  Client = resultsByClient.Key,
                  DebtTypes = null,
             }).ToList();

产生 1300 个客户。接下来我尝试了这个:

output.Clients = (from results in res
             group results by results.Client.ClientID into resultsByClient
             select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType()
             {
                  Client = new Client { ClientID = resultsByClient.Key },
                  DebtTypes = null,
             }).ToList();

这产生了一个客户(万岁!)。除非我丢失了所有客户信息(嘘!) 猜测是因为它是通过参考而不是内容来比较客户,所以我写了以下内容:

public partial class Client
{
    public static bool operator ==(Client left, Client right)
    {
        return left.ClientID == right.ClientID;
    }

    public static bool operator !=(Client left, Client right)
    {
        return left.ClientID != right.ClientID;
    }

    public override int GetHashCode()
    {
        return ClientID;
    }
}

那什么也没做。它反复调用 GetHashCode(),我捏造了它以强制它为任何匹配的 ClientID 返回相同的哈希代码,但它仍然创建了 1300 个客户端组。

问候,

詹姆斯。

--------### UPDATE 2 ###-------

好吧,我想我应该尝试让 Linq2Sql 只输出简单的值进行分组:

g.Key.ClientID,
g.Key.ClientName1,
g.Key.DebtTypeID,
g.Key.DebtTypeShortDesc,
g.Key.DebtTypeLongDesc,

然后将测试 Linq2Objects 更改为:

output.Clients = (from results in res
              group results by new { ClientID = results.ClientID, DisplayName = results.ClientName1 } into resultsByClient
              select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType()
              {
                  Client = new Client { ClientID = resultsByClient.Key.ClientID, DisplayName = resultsByClient.Key.DisplayName },
                  DebtTypes = null,
              }).ToList();

有效。因此,匿名类型按照我希望的方式进行比较,通过内容而不是引用(显然) 这不会:

output.Clients = (from results in res
              group results by new SiDemClient { ClientID = results.ClientID, DisplayName = results.ClientName1 } into resultsByClient
              select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType()
              {
                  Client = resultsByClient.Key,//new Client { ClientID = resultsByClient.Key.ClientID, DisplayName = resultsByClient.Key.DisplayName },
                  DebtTypes = null,
              }).ToList();

仍然会创建 1300 个组。

因此,匿名类型以一种我不理解的神奇方式进行比较。如何使我的 Client 类像匿名类型一样进行比较?

问候,

詹姆斯。

--------### 找到解决方案 ###-------

--------### 非常感谢 Enigmativity ###- ------

我需要重写 Equals() 方法而不是实现 == 运算符。 现在分组工作正常了,我有一个很棒的 Xml 文档需要重新显示!

public partial class SiDemClient
{
    public override bool Equals(object obj)
    {
        if (obj is SiDemClient)
        {
            return this.ClientID.Equals(((SiDemClient)obj).ClientID);
        }
        return false;
    }


    public override int GetHashCode()
    {
        return ClientID;
    }
}

非常感谢,

詹姆斯。

I'm trying to work with grouped data coming back from SQL.
The method I'm writing is to provide the data for a "Case Status Overview" screen.
It must produce a nested XML document.

Now, I could do it the easy way, but I'm trying to learn whether it's possible to use the linq "group by" statement and then to project the data already nested. (the easy way would be just to pull back the data in a tabular fashion from the database and then for-loop through it forming the Xml document for output)

Here is the data hierarchy:

Every Case has a DebtType and every DebtType has a Client.

Here is the SQL that retrieves the data:

SELECT   ClientNames.ClientID                                             ,
         ClientNames.ClientCode                                           ,
         ClientNames.ClientName                                           ,
         DebtTypes.DebtTypeID                                             ,
         DebtTypes.DebtTypeShortDesc                                      ,
         DebtTypes.DebtTypeLongDesc                                       ,
         Cases.CurrentStateCode                                           ,
         SUM(1 - CAST(Cases.CaseClosed AS INT))  AS OpenCaseCount         ,
         SUM(CAST(Cases.CaseClosed AS     INT))  AS ClosedCaseCount       ,
         SUM(CAST(Cases.CaseOnHold AS     INT))  AS OnHoldCaseCount       ,
         SUM(CAST(Cases.CaseReferred AS   INT))  AS ReferredCaseCount     ,
         COUNT(Cases.CaseID)                     AS TotalCaseCount        ,
         SUM(Cases.CaseTotalPaid)                AS TotalAmountPaid       ,
         SUM(Cases.CaseCurrentOutstandingAmount) AS TotalAmountOutstanding,
         SUM(Cases.CaseTotalDebtWrittenOff)      AS TotalAmountWrittenOff ,
         SUM(Cases.CaseTotalDebtCancelled)       AS TotalAmountCancelled
FROM     ClientNames
         INNER JOIN ClientDebtTypes
         ON       ClientNames.ClientID = ClientDebtTypes.ClientID
         INNER JOIN DebtTypes
         ON       ClientDebtTypes.DebtTypeID = DebtTypes.DebtTypeID
         INNER JOIN Cases
         ON       ClientDebtTypes.ClientDebtTypeID = Cases.CaseClientDebtTypeID
GROUP BY ClientNames.ClientID       ,
         ClientNames.ClientCode     ,
         ClientNames.ClientName     ,
         DebtTypes.DebtTypeID       ,
         DebtTypes.DebtTypeShortDesc,
         DebtTypes.DebtTypeLongDesc ,
         Cases.CurrentStateCode
ORDER BY ClientNames.ClientID,
         DebtTypes.DebtTypeID,
         CurrentStateCode

Using Linqer it converts it to:

from clientnames in db.ClientNames
join clientdebttypes in db.ClientDebtTypes on clientnames.ClientID equals clientdebttypes.ClientID
join debttypes in db.DebtTypes on clientdebttypes.DebtTypeID equals debttypes.DebtTypeID
join cases in db.Cases on new { ClientDebtTypeID = clientdebttypes.ClientDebtTypeID } equals new { ClientDebtTypeID = cases.CaseClientDebtTypeID }
group new {clientnames, debttypes, cases} by new {
  clientnames.ClientID,
  clientnames.ClientCode,
  clientnames.ClientName1,
  debttypes.DebtTypeID,
  debttypes.DebtTypeShortDesc,
  debttypes.DebtTypeLongDesc,
  cases.CurrentStateCode
} into g
orderby
  g.Key.ClientID,
  g.Key.DebtTypeID,
  g.Key.CurrentStateCode
select new {
  ClientID = (System.Int32?)g.Key.ClientID,
  g.Key.ClientCode,
  g.Key.ClientName1,
  DebtTypeID = (System.Int32?)g.Key.DebtTypeID,
  g.Key.DebtTypeShortDesc,
  g.Key.DebtTypeLongDesc,
  g.Key.CurrentStateCode,
  OpenCaseCount = (System.Int64?)g.Sum(p => 1 - Convert.ToInt32(p.cases.CaseClosed)),
  ClosedCaseCount = (Int32?)g.Sum(p => Convert.ToInt32(p.cases.CaseClosed)),
  OnHoldCaseCount = (Int32?)g.Sum(p => Convert.ToInt32(p.cases.CaseOnHold)),
  ReferredCaseCount = (Int32?)g.Sum(p => Convert.ToInt32(p.cases.CaseReferred)),
  TotalCaseCount = (Int64?)g.Count(p => p.cases.CaseID != null),
  TotalAmountPaid = (System.Decimal?)g.Sum(p => p.cases.CaseTotalPaid),
  TotalAmountOutstanding = (System.Decimal?)g.Sum(p => p.cases.CaseCurrentOutstandingAmount),
  TotalAmountWrittenOff = (System.Decimal?)g.Sum(p => p.cases.CaseTotalDebtWrittenOff),
  TotalAmountCancelled = (System.Decimal?)g.Sum(p => p.cases.CaseTotalDebtCancelled)
}

Now as I mentioned, I could stop there and right a for loop to create the Xml data.
But I'm trying to create a nested group (IGrouping<ClientName,IGrouping<DebtType,SummaryClass>>)
and then project the data in a nested format.

Now we're using LinqToXsd to create strong type wrappers for out Xml documents, but essentially all this means is that out output type is:

private class ClientSummary
{
    public string ClientName { get; set; }
    public IList<DebtTypeSummary> DebtTypes { get; set; }
}

private class DebtTypeSummary
{
    public string DebtType { get; set; }
    public IList<StateCodeSummary> StateCodes { get; set; }
}

private class StateCodeSummary
{
    public string StateCode { get; set; }
    public int TotalCount { get; set; }
    public decimal TotalAmountPaid { get; set; }
    //etc
    //etc
    //etc
}

Now I got as far as writing the following Linq:

var grouping = from cases in db.Cases
              join clientdebttypes in db.ClientDebtTypes on cases.CaseClientDebtTypeID equals clientdebttypes.ClientID
              join debttypes in db.DebtTypes on clientdebttypes.DebtTypeID equals debttypes.DebtTypeID
              group cases by new ClientDebtTypePair() { ClientDebtType = clientdebttypes, DebtType = debttypes } into casesByClientDebtTypes
              join clientnames in db.ClientNames on casesByClientDebtTypes.Key.ClientDebtType.ClientName equals clientnames
              group casesByClientDebtTypes by clientnames;

var projected = from casesByClientDebtTypes in grouping
            let client = casesByClientDebtTypes.Key
            select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType()
              {
                  Client = new Client()
                  {
                      ClientID = client.ClientID,
                      DisplayName = client.ClientName1,
                  },
                  DebtTypes = from cases in casesByClientDebtTypes
                              let debttype = cases.Key.DebtType
                              select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType.DebtTypesLocalType()
                              {
                                   DebtType = new DebtType()
                                   {
                                        DebtTypeID = debttype.DebtTypeID,
                                         Description = debttype.DebtTypeLongDesc,
                                          DisplayName = debttype.DebtTypeShortDesc,
                                   },
                                    StatesCodes = from cases2 in cases
                                                  select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType.DebtTypesLocalType.StatesCodesLocalType()
                                                  {
                                                       ClosedCasesCount = cases2.Sum(p => Convert.ToInt32(p.cases.CaseClosed))

which joins and groups the database tables and then tries to project the result a ClientSummary (the class names are different but that's because the above is a simplified view of the output classes). I fail completely when I've drilled all the way down to the Cases table and I find that I don't really understand how to do agregate functions. They appear to only be available on IGrouping<K, T>s and it seems I've just got confused.

I need to also ensure that the summaries are calculated server side, pulling back millions of cases would be bad.

Can anybody help me with this one? Is this even possible?

Regards,

James.

-------### UPDATE 1 ###-------

OK, been working on this again today.
I decided to use Linq2SQL to pull pack 2D data and then reformat it using Linq2Objects.

Here is what I started with:

var sql = from clientnames in db.ClientNames
      join clientdebttypes in db.ClientDebtTypes on clientnames.ClientID equals clientdebttypes.ClientID
      join debttypes in db.DebtTypes on clientdebttypes.DebtTypeID equals debttypes.DebtTypeID
      join cases in db.Cases on new { ClientDebtTypeID = clientdebttypes.ClientDebtTypeID } equals new { ClientDebtTypeID = cases.CaseClientDebtTypeID }
      group new { clientnames, debttypes, cases } by new
      {
          clientnames.ClientID,
          clientnames.ClientCode,
          clientnames.ClientName1,
          debttypes.DebtTypeID,
          debttypes.DebtTypeShortDesc,
          debttypes.DebtTypeLongDesc,
          cases.CurrentStateCode
      } into g
      orderby
        g.Key.ClientID,
        g.Key.DebtTypeID,
        g.Key.CurrentStateCode
      select new
      {
          Client = new Client{ ClientID = g.Key.ClientID, DisplayName = g.Key.ClientName1 },
          DebtType = new DebtType{ DebtTypeID = g.Key.DebtTypeID, DisplayName = g.Key.DebtTypeShortDesc, Description = g.Key.DebtTypeLongDesc },
          StateSummary = new LoadCaseStatusOverviewScreenOutput.ClientsLocalType.DebtTypesLocalType.StatesCodesLocalType()
          {
              StateCode = g.Key.CurrentStateCode,
              OpenCasesCount = g.Sum(p => 1 - Convert.ToInt32(p.cases.CaseClosed)),
              ClosedCasesCount = g.Sum(p => Convert.ToInt32(p.cases.CaseClosed)),
              OnHoldCasesCount = g.Sum(p => Convert.ToInt32(p.cases.CaseOnHold)),
              ReferredCasesCount = g.Sum(p => Convert.ToInt32(p.cases.CaseReferred)),
              TotalCasesCount = g.Count(p => p.cases.CaseID != null),
              TotalAmountPaid = g.Sum(p => p.cases.CaseTotalPaid),
              TotalAmountOutstanding = g.Sum(p => p.cases.CaseCurrentOutstandingAmount),
              TotalAmountWrittenOff = g.Sum(p => p.cases.CaseTotalDebtWrittenOff),
              TotalAmountCancelled = g.Sum(p => p.cases.CaseTotalDebtCancelled),
          }
      };
var res = sql.ToList();

output.Clients = (from results in res
              group results by results.Client into resultsByClient
              from resultsByDebtType in
                  (from results in resultsByClient
                   group results by results.DebtType)
              group resultsByDebtType by resultsByClient.Key into resultsByDebtTypeByClient
              select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType()
              {
                  Client = resultsByDebtTypeByClient.Key,
                  DebtTypes = (from resultsByDebtType in resultsByDebtTypeByClient
                               select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType.DebtTypesLocalType()
                               {
                                   DebtType = resultsByDebtType.Key,
                                   StatesCodes = (from results in resultsByDebtType
                                                  let summary = results.StateSummary
                                                  select results.StateSummary).ToList()
                               }).ToList()
              }).ToList();

That runs, but produces one Client/DebtType/Summary set for every result. So even though there is only one client in this case, I end up with 1300 clients, all identical.
I simplified it to the following:

output.Clients = (from results in res
             group results by results.Client into resultsByClient
             select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType()
             {
                  Client = resultsByClient.Key,
                  DebtTypes = null,
             }).ToList();

That produces 1300 clients. Next I tried this:

output.Clients = (from results in res
             group results by results.Client.ClientID into resultsByClient
             select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType()
             {
                  Client = new Client { ClientID = resultsByClient.Key },
                  DebtTypes = null,
             }).ToList();

And THAT produces ONE client (hurray!). Except I loose all the client information (boo!)
Guessing that as it's comparing client by refernce instead of by content I wrote the following:

public partial class Client
{
    public static bool operator ==(Client left, Client right)
    {
        return left.ClientID == right.ClientID;
    }

    public static bool operator !=(Client left, Client right)
    {
        return left.ClientID != right.ClientID;
    }

    public override int GetHashCode()
    {
        return ClientID;
    }
}

That did nothing. It repeatedly calls GetHashCode(), which I fudged to force it to return the same hash code for any matching ClientID, but it still created 1300 Client groups.

Regards,

James.

-------### UPDATE 2 ###-------

OK, I thought I would have a go at making the Linq2Sql output only simple values for grouping by:

g.Key.ClientID,
g.Key.ClientName1,
g.Key.DebtTypeID,
g.Key.DebtTypeShortDesc,
g.Key.DebtTypeLongDesc,

And then changed the test Linq2Objects to:

output.Clients = (from results in res
              group results by new { ClientID = results.ClientID, DisplayName = results.ClientName1 } into resultsByClient
              select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType()
              {
                  Client = new Client { ClientID = resultsByClient.Key.ClientID, DisplayName = resultsByClient.Key.DisplayName },
                  DebtTypes = null,
              }).ToList();

That works. So anonymous types compare in the way I want them to, by content not reference (apparently)
This does not:

output.Clients = (from results in res
              group results by new SiDemClient { ClientID = results.ClientID, DisplayName = results.ClientName1 } into resultsByClient
              select new LoadCaseStatusOverviewScreenOutput.ClientsLocalType()
              {
                  Client = resultsByClient.Key,//new Client { ClientID = resultsByClient.Key.ClientID, DisplayName = resultsByClient.Key.DisplayName },
                  DebtTypes = null,
              }).ToList();

That still creates 1300 groups.

So, anonymous types compare in a magical way that I don't understand. How can I make my Client class compare like an anonymous type?

Regards,

James.

-------### SOLUTION FOUND ###-------

-------### MANY THANKS TO Enigmativity ###-------

I needed to override the Equals() method instead of implementing the == operator.
Now the grouping works and I have a wonderful Xml document to reutrn!

public partial class SiDemClient
{
    public override bool Equals(object obj)
    {
        if (obj is SiDemClient)
        {
            return this.ClientID.Equals(((SiDemClient)obj).ClientID);
        }
        return false;
    }


    public override int GetHashCode()
    {
        return ClientID;
    }
}

Many Thanks,

James.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离鸿 2024-12-08 04:05:20

当您重写GetHashCode时,您还必须重写Equals== & != 运算符是无关紧要的。

尝试一下:

public partial class Client
{
    public override bool Equals(object obj)
    {
        if (obj is Client)
        {
            return this.ClientID.Equals(((Client)obj).ClientID);
        }
        return false;
    }

    public override int GetHashCode()
    {
        return this.ClientID.GetHashCode();
    }
}

看看是否有帮助。

When you override GetHashCode you must also override Equals. The == & != operators are irrelevant.

Try with this:

public partial class Client
{
    public override bool Equals(object obj)
    {
        if (obj is Client)
        {
            return this.ClientID.Equals(((Client)obj).ClientID);
        }
        return false;
    }

    public override int GetHashCode()
    {
        return this.ClientID.GetHashCode();
    }
}

See if that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文