ASP.Net C#相当于Coldfusion cfoutput GROUP

时间:2014-12-12 21:48:43

标签: c# asp.net coldfusion

我们目前正在将Coldfusion网站转换为ASP.NET C#。我们有一份报告,它使用3级cfoutput GROUP(按年分组,然后按代理,然后由律师分组,并获得分配给他们的案例总数)。

我很难找到如何将其转换为ASP.NET的示例。

<cfquery name="getRecords" datasource="datasource">
    select datepart(yyyy, dtReceived) as [year], agencies.code, users.lname, users.fname        
    from cases left outer join users on cases.users_id=users.users_id
        left outer join agencies on cases.agencies_id = agencies.agencies_id   
    order by datepart(yyyy,dtReceived) desc,agencies.code, lname,fname
</cfquery>

<table>
<cfoutput query="getRecords" group="YEAR">
    <tr>
        <td>#YEAR#</td>
        <td>Office</td>
        <td>## Attorney(s)</td>
        <td>## Suspense(s)</td>
    </tr>    
    <cfoutput group="CODE">         
        <tr>
            <td>&nbsp;</td>
            <td>#htmleditformat(CODE)#</td>         
            <cfset i = 0>
            <cfoutput group="LNAME">
                <cfset i = i+1>
            </cfoutput>

            <td>#i#</td>

            <cfset j = 0>
            <cfoutput>
                <cfset j = j+1>
            </cfoutput>

            <td><strong>#j#</td>
        </tr>   

    <cfoutput group="LNAME">
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>#htmleditformat(LNAME)#, #htmleditformat(FNAME)#</td>

                <cfset h = 0>
                <cfoutput>
                    <cfset h = h+1>
                </cfoutput>

                <td>#h#</td>
            </tr>
            </cfoutput>

    <tr>
        <td colspan="4">&nbsp;<br>&nbsp;</td>
    </tr>
    </cfoutput>


</cfoutput>
</table>

2 个答案:

答案 0 :(得分:1)

不如ColdFusion好,但您可以使用linq对结果进行分组。

假设您有一个像这样的对象列表

List<Case> Cases = new List<Case>() 
        {
            new Case(){ Year = 2001, Agency = "Agency1", Attorney="Atticus Finch", CaseNumber = 1},
            new Case(){ Year = 2001, Agency = "Agency1", Attorney="Atticus Finch", CaseNumber = 2},
            new Case(){ Year = 2001, Agency = "Agency1", Attorney="Ben Matlock", CaseNumber = 3},
            new Case(){ Year = 2002, Agency = "Agency1", Attorney="Atticus Finch", CaseNumber = 99},
            new Case(){ Year = 2002, Agency = "Agency1", Attorney="Ben Matlock", CaseNumber = 22},
            new Case(){ Year = 2002, Agency = "Agency2", Attorney="Johnny Cochran", CaseNumber = 12},
            new Case(){ Year = 2003, Agency = "Agency2", Attorney="Mark Geragos", CaseNumber = 14},
            new Case(){ Year = 2003, Agency = "Agency3", Attorney="Robert Shapiro", CaseNumber = 29}
        };

public class Case {

    public int CaseNumber { get; set; }
    public int Year { get; set; }
    public string Agency { get; set; }
    public string Attorney { get; set; }

}

您可以使用Linq进行一些分组。

var caseList = from c in Cases
                   group c by new { c.Year } into yrgrp
                   orderby yrgrp.Key.Year
                   select new
                   {
                       Year = yrgrp.Key.Year,
                       Agencies = from d in yrgrp
                                  group d by new { d.Agency} into agencygrp
                                  select new
                                  {
                                      Agency = agencygrp.Key.Agency,
                                      Total = agencygrp.Count(),
                                      Attorneys = from e in agencygrp
                                                  group e by e.Attorney into attorneygrp
                                                  select new 
                                                  {
                                                     Attorney = attorneygrp.Key,
                                                     Cases = attorneygrp,
                                                     Total = attorneygrp.Count()

                                                  }

                                  }
                   };

我承认它看起来不漂亮。

然后,您可以遍历新列表,并将其输出到表格中,或者仅在我的示例中输出到控制台。

foreach (var item in caseList)
    {
        Console.WriteLine(item.Year);
        foreach (var agency in item.Agencies)
        {
            Console.WriteLine( agency.Agency + " Number Cases:" + agency.Total + " Number Attorneys:" + agency.Attorneys.Count());

            foreach (var attorney in agency.Attorneys)
            {
                Console.WriteLine(attorney.Attorney + " Number Cases: " + attorney.Total );

                foreach (var cases in attorney.Cases)
                {
                    Console.WriteLine("Case #" + cases.CaseNumber);
                }
            }

        }
    }
        Console.ReadKey();

    }

这输出..

2001
Agency1 Number Cases:3 Number Attorneys:2
Atticus Finch Number Cases: 2
Case #1
Case #2
Ben Matlock Number Cases: 1
Case #3
2002
Agency1 Number Cases:2 Number Attorneys:2
Atticus Finch Number Cases: 1
Case #99
Ben Matlock Number Cases: 1
Case #22
Agency2 Number Cases:1 Number Attorneys:1
Johnny Cochran Number Cases: 1
Case #12
2003
Agency2 Number Cases:1 Number Attorneys:1
Mark Geragos Number Cases: 1
Case #14
Agency3 Number Cases:1 Number Attorneys:1
Robert Shapiro Number Cases: 1
Case #29

答案 1 :(得分:0)

根据我在网上找到的内容,ASP不会反映此功能。可能有最新版本,但我无法找到任何相关内容。

在没有此功能的语言中,这是复制它的一种方法。 (它并不是一个漂亮的,但每种语言都有其优点和缺点。)这是部分伪造的代码,所以它不会像任何语言那样起作用。

var cYear = "";
var cAgency = "":
var cAtt = "";
[output loop] {
    if (cYear != GetRecords.Year) {
        // output tr showing GetRecords.Year
        cYear = GetRecords.Year;
        cAgency = ""; // Set these to blank in case the next row starts with thee same as the last finishes with. Otherwise the display would be screwed up.
        cAtt = "";
    } 
    if (cAgency != GetRecords.Agency) {
        // output tr showing GetRecords.Agency
        cAgency = GetRecords.Agency;
        cAtt = "";
    }
    if (cAtt != GetRecords.Attorney} {
        cAtt = GetRecords.Attorney;
        // output tr showing GetRecords.Attorney.
    }

    //output the data row
}

每个层重置与下一层相关的值,以便如果最后一个父组的最后一行与代理或律师匹配,则不会搞砸显示。

关于您为群组进行的计数,您可以使用子查询上的count()在查询中执行此操作,以获取您正在查找的计数。即使在CF中,这样简单的计数也很容易(就所需的代码量而言),最好是在代码端进行。