【问题标题】:How to pass multiple list types as a parameter using the same method variable如何使用相同的方法变量将多个列表类型作为参数传递
【发布时间】:2014-05-22 19:59:22
【问题描述】:

我正在尝试使用相同的方法变量将多个列表类型作为参数传递,然后根据过去的类型循环遍历类型。我尝试使用通用方法,但它不起作用。以下是伪/示例代码。 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

            }            
        }

感谢您的帮助。

【问题讨论】:

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


    【解决方案1】:

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

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

    然后在 String.Format 中将“value.TA01”更改为“TA01”。对所有其他变量执行相同操作。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-15
      相关资源
      最近更新 更多