如何使用相同的方法变量将多个列表类型作为参数传递

时间:2014-05-16 15:21:19

标签: c#-4.0 entity-framework-4

我尝试使用相同的方法变量将多个列表类型作为参数传递,然后根据过去的类型循环遍历类型。我尝试使用通用方法,但它不起作用。以下是伪/示例代码。 List SAS_F_DI​​SAGG_F和List SAS_C_DISAGG_C是SQL / Entity,List DisaggReportGroups是类对象。我试图传递实体列表。

protected void GetReportGroup()
        {

           DisaggReportGroups rptGroup = new DisaggReportGroups();
           List<DisaggReportGroups> disagreportGroup = new List<DisaggReportGroups>();
           disagreportGroup.Add(rptGroup);

           DisaggregatedReportData disagReportData = new DisaggregatedReportData();

            foreach (var reportGroup in disagreportGroup)
            {

                if (reportGroup.FuturesOnly == "Futures Only, " & reportGroup.Agriculture == "Agriculture")
                {

                    List<SAS_F_DISAGG_F> futONlyDisagReportData = disagReportData.GetFuturesOnlyReportData(reportGroup.Agriculture).ToList();

                    CreateLongFormatReport<List<SAS_F_DISAGG_F>>(reportGroup.AgricultureFilenameFOLF, reportGroup.FuturesOnly, reportGroup.Agriculture, futONlyDisagReportData);

                }

                else if (reportGroup.FOCombined == "Futures and Options Combined, " & reportGroup.Agriculture == "Agriculture")
                {
                    List<SAS_C_DISAGG_C> combinedDisagReportData = disagReportData.GetFOCombinedReportData(reportGroup.Agriculture).ToList();

                    CreateLongFormatReport<List<SAS_C_DISAGG_C>>(reportGroup.AgricultureFilenameFOCombinedLF, reportGroup.FOCombined, reportGroup.Agriculture, combinedDisagReportData);
                }


            }

        }



protected void CreateFormatReport<T>(string filename, string disagCategory, string commSubGp, List<T> reportData) 
        {                                                       
            using (FileStream fileStream = new FileStream(Server.MapPath(@"~/Includes/") + filename, FileMode.Create))            
            {
                using (StreamWriter writer = new StreamWriter(fileStream))
                {

                    foreach (var value in reportData)
                    {

                        string FuturesOnly = "Futures Only, ";
                        string FOCombined = "Futures and Options Combined, ";
                        string reportCategory = "";


                        if (disagCategory == FuturesOnly)
                        {

                            reportCategory = FuturesOnly;
                        }
                        else if (disagCategory == FOCombined)
                        {
                            reportCategory = FOCombined;
                        }

                        string row01 = String.Format("{0, -10}{1, 29}{2, 8}", value.MKTTITL.PadRight(120), "Code -", value.Conmkt);

                        string row02 = String.Format("{0, -10}{1, 7}{2, 14}", "Blah Blah - ", reportCategory, value.DAT1TITL);


                        string row03 = String.Format("{0, 3}{1, 3}{2, 8:0,0}{3, 3}{4, 8:0,0}{5, 11:0,0}{6, 11:0,0}{7, 11:0,0}{8, 11:0,0}{9, 13:0,0}{10, 11:0,0}{11, 11:0,0}{12, 13:0,0}{13, 10:0,0}{14, 9:0,0}{15, 3}{16, 8:0,0}{17, 10:0,0}", "All",
                       colon, value.TA01, colon, value.TA02, value.TA03, value.TA04, value.TA05, value.TA06, value.TA07, value.TA08, value.TA09, value.TA10, value.TA11, value.TA12, colon, value.TA15, value.TA16);

                        string row04 = String.Format("{0, 3}{1, 3}{2, 8:0,0}{3, 3}{4, 8:0,0}{5, 11:0,0}{6, 11:0,0}{7, 11:0,0}{8, 11:0.##}{9, 13:0,0}{10, 11:0,0}{11, 11:0,0}{12, 13:0,0}{13, 10:0,0}{14, 9:0,0}{15, 3}{16, 8:0,0}{17, 10:0,0}", "Old",
                        colon, value.TO01, colon, value.TO02, value.TO03, value.TO04, value.TO05, value.TO06, value.TO07, value.TO08, value.TO09, value.TO10, value.TO11, value.TO12, colon, value.TO15, value.TO16);


                        writer.Write(row01);                        
                        writer.WriteLine(row02);
                        writer.WriteLine(row03);
                        writer.WriteLine(row04);


                    } //end foreach
                    writer.Close();
                } //end of stream writer

            }            
        }

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我自己设法解决了这个问题,所以我为其他可能需要相同类型帮助的人发布我的解决方案。解决方案是在foreach迭代中使用Reflection。

foreach (var value in ReportData)
{
    //Reflection can be used
    string TA01 = value.GetType().GetProperty("TA01").GetValue(value).ToString();
    //...
    //...
    //do more stuff/coding...
}

然后在String.Format中更改&#34; value.TA01&#34;到&#34; TA01&#34;。对所有其他变量都这样做。

希望得到这个帮助。