这个问题是这个问题的一个衍生问题(可以将此方法重构为使用 Lamba 吗?)。在对该问题的评论中提出了这一点:
重构需要在不同的地方进行。糟糕的不是你的函数,而是
CLMExplorerHistory
.内部数据结构不需要以 1:1 的方式表示 CSV 文件的结构。只需确保您可以以兼容的方式进行读写。
我认为您正在尝试直接使用 csv 数据结构,而不是查看可能的重复区域,这些区域表明实现类类型的明显位置以允许代码重用。您能否为您的 CSV 文件发布一段示例数据以表明您的起点?
在这些评论的后端,这里是示例 CSV 数据的片段:
Date,Status,Weekly Bible Reading,Song1 #,Song1 Title,Song1 Scripture,Song2 #,Song2 Title,Song2 Scripture,Song3 #,Song3 Title,Song3 Scripture,Meeting?,CO Visit,Cancel Reason,# Classes,Chairman,PrayerOpen,PrayerClose,TreasuresTalk,TreasuresTalk_Theme,TreasuresDigging,BibleReading_A,BibleReading_Study_A,BibleReading_B,BibleReading_Study_B,BibleReading_C,BibleReading_Study_C,BibleReading_Source,Apply1_Description,Apply2_Description,Apply3_Description,Apply4_Description,Apply1_A,Apply1_Asst_A,Apply1_Study_A,Apply2_A,Apply2_Asst_A,Apply2_Study_A,Apply3_A,Apply3_Asst_A,Apply3_Study_A,Apply4_A,Apply4_Asst_A,Apply4_Study_A,Apply1_B,Apply1_Asst_B,Apply1_Study_B,Apply2_B,Apply2_Asst_B,Apply2_Study_B,Apply3_B,Apply3_Asst_B,Apply3_Study_B,Apply4_B,Apply4_Asst_B,Apply4_Study_B,Apply1_C,Apply1_Asst_C,Apply1_Study_C,Apply2_C,Apply2_Asst_C,Apply2_Study_C,Apply3_C,Apply3_Asst_C,Apply3_Study_C,Apply4_C,Apply4_Asst_C,Apply4_Study_C,LivingPart1,LivingPart1_Theme,LivingPart1_Length,LivingPart2,LivingPart2_Theme,LivingPart2_Length,CBS,CBS_Source,CBS_Read,Audience B,Audience C,AuxCounselor B,AuxCounselor C
01/03/2021,Registrato e completo,NUMERI 7-8,4,“Geova è il mio Pastore”,Salmo 23,54,“Questa è la via”,"Isaia 30:20, 21",127,Che tipo di persona sono?,2 Pietro 3:11,Y,N,,1,Italo De Gaeta,xxx,xxx,xxx,“L’accampamento d’Israele: lezioni utili”,xxx,xxx,5,,,,,Nu 7:1-17,Commemorazione,Visita ulteriore,Visita ulteriore,Visita ulteriore,xxx,xxx,11,xxx,xxx,6,xxx,xxx,12,xxx,xxx,17,,,,,,,,,,,,,,,,,,,,,,,,,xxx,Risultati raggiunti dall’organizzazione,(5 min),xxx,Bisogni locali,(10 min),xxx,"rr cap. 5 parr. 17-22, riquadro 5A",xxx,,,,
我使用CsvReader
该类将其读入CLMExplorerHistory
对象。该类定义如下:
public class CLMExplorerHistory
{
[Format("dd/MM/yyyy")]
[Name("Date")]
public DateTime Date { get; set; }
[BooleanFalseValues(new string[] { "N", "n" })]
[BooleanTrueValues(new string[] { "Y", "y" })]
[Name("Meeting?")]
public bool Meeting { get; set; }
[Name("# Classes")]
public int Classes { get; set; }
[Name("Chairman")]
public string Chairman { get; set; }
[Name("AuxCounselor B")]
public string AuxCounsellor1 { get; set; }
[Name("AuxCounselor C")]
public string AuxCounsellor2 { get; set; }
[Name("PrayerOpen")]
public string PrayerOpen { get; set; }
[Name("PrayerClose")]
public string PrayerClose { get; set; }
[Name("CBS")]
public string CBSConductor { get; set; }
[Name("CBS_Read")]
public string CBSReader { get; set; }
[Name("TreasuresTalk")]
public string TreasuresTalkName { get; set; }
[Name("TreasuresTalk_Theme")]
public string TreasuresTalkTheme { get; set; }
[Name("TreasuresDigging")]
public string SpiritualGemsName { get; set; }
[Name("LivingPart1")]
public string LivingPart1Name { get; set; }
[Name("LivingPart1_Theme")]
public string LivingPart1Theme { get; set; }
[Name("LivingPart2")]
public string LivingPart2Name { get; set; }
[Name("LivingPart2_Theme")]
public string LivingPart2Theme { get; set; }
[Name("BibleReading_A")]
public string BibleReadingClass1Name { get; set; }
[Name("BibleReading_B")]
public string BibleReadingClass2Name { get; set; }
[Name("BibleReading_C")]
public string BibleReadingClass3Name { get; set; }
[Name("BibleReading_Study_A")]
public string BibleReadingStudy { get; set; }
[Name("Apply1_Description")]
public string StudentItem1Description { get; set; }
[Name("Apply2_Description")]
public string StudentItem2Description { get; set; }
[Name("Apply3_Description")]
public string StudentItem3Description { get; set; }
[Name("Apply4_Description")]
public string StudentItem4Description { get; set; }
[Name("Apply1_A")]
public string StudentItem1Class1StudentName { get; set; }
[Name("Apply1_B")]
public string StudentItem1Class2StudentName { get; set; }
[Name("Apply1_C")]
public string StudentItem1Class3StudentName { get; set; }
[Name("Apply1_Asst_A")]
public string StudentItem1Class1AssistantName { get; set; }
[Name("Apply1_Asst_B")]
public string StudentItem1Class2AssistantName { get; set; }
[Name("Apply1_Asst_C")]
public string StudentItem1Class3AssistantName { get; set; }
[Name("Apply2_A")]
public string StudentItem2Class1StudentName { get; set; }
[Name("Apply2_B")]
public string StudentItem2Class2StudentName { get; set; }
[Name("Apply2_C")]
public string StudentItem2Class3StudentName { get; set; }
[Name("Apply2_Asst_A")]
public string StudentItem2Class1AssistantName { get; set; }
[Name("Apply2_Asst_B")]
public string StudentItem2Class2AssistantName { get; set; }
[Name("Apply2_Asst_C")]
public string StudentItem2Class3AssistantName { get; set; }
[Name("Apply3_A")]
public string StudentItem3Class1StudentName { get; set; }
[Name("Apply3_B")]
public string StudentItem3Class2StudentName { get; set; }
[Name("Apply3_C")]
public string StudentItem3Class3StudentName { get; set; }
[Name("Apply3_Asst_A")]
public string StudentItem3Class1AssistantName { get; set; }
[Name("Apply3_Asst_B")]
public string StudentItem3Class2AssistantName { get; set; }
[Name("Apply3_Asst_C")]
public string StudentItem3Class3AssistantName { get; set; }
[Name("Apply4_A")]
public string StudentItem4Class1StudentName { get; set; }
[Name("Apply4_B")]
public string StudentItem4Class2StudentName { get; set; }
[Name("Apply4_C")]
public string StudentItem4Class3StudentName { get; set; }
[Name("Apply4_Asst_A")]
public string StudentItem4Class1AssistantName { get; set; }
[Name("Apply4_Asst_B")]
public string StudentItem4Class2AssistantName { get; set; }
[Name("Apply4_Asst_C")]
public string StudentItem4Class3AssistantName { get; set; }
[Name("Apply1_Study_A")]
public string StudentItem1Study { get; set; }
[Name("Apply2_Study_A")]
public string StudentItem2Study { get; set; }
[Name("Apply3_Study_A")]
public string StudentItem3Study { get; set; }
[Name("Apply4_Study_A")]
public string StudentItem4Study { get; set; }
}
我像这样读入 CSV 文件:
using (var reader = new StreamReader(_calendarDBPath))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
var records = csv.GetRecords<CLMExplorerHistory>();
foreach (var record in records)
{
...
目前,CSV 记录已读入此公共属性列表。我想知道它是否可以直接将字段读入更复杂的对象?
具体来说,该# Classes
字段的值为 1、2 或 3。对于这些类中的每一个(不是 C# 类 - 想想school),都有类似的数据。
每个班级有 4 个项目,每个班级的描述/研究都是相同的。所以这些 CSV 字段很常见:
[Name("Apply1_Description")]
[Name("Apply2_Description")]
[Name("Apply3_Description")]
[Name("Apply4_Description")]
[Name("BibleReading_Study_A")]
[Name("Apply1_Study_A")]
[Name("Apply2_Study_A")]
[Name("Apply3_Study_A")]
[Name("Apply4_Study_A")]
但其余的是特定于 3 个班级(学校):
- 这些字段属于第 1 类:
[Name("BibleReading_A")]
[Name("Apply1_A")]
[Name("Apply1_Asst_A")]
[Name("Apply2_A")]
[Name("Apply2_Asst_A")]
[Name("Apply3_A")]
[Name("Apply3_Asst_A")]
[Name("Apply4_A")]
[Name("Apply4_Asst_A")]
- 这些字段属于第 2 类:
[Name("BibleReading_B")]
[Name("Apply1_B")]
[Name("Apply1_Asst_B")]
[Name("Apply2_B")]
[Name("Apply2_Asst_B")]
[Name("Apply3_B")]
[Name("Apply3_Asst_B")]
[Name("Apply4_B")]
[Name("Apply4_Asst_B")]
- 这些字段属于第 3 类:
[Name("BibleReading_C")]
[Name("Apply1_C")]
[Name("Apply1_Asst_C")]
[Name("Apply2_C")]
[Name("Apply2_Asst_C")]
[Name("Apply3_C")]
[Name("Apply3_Asst_C")]
[Name("Apply4_C")]
[Name("Apply4_Asst_C")]
这 3 个类可以由 3 个“StudentClass”对象表示Lists
。就像是
StudentClasses
NumClasses
Item1Desc
Item2Desc
Item3Desc
Item4Desc
BibleReadingStudy
Item1Study
Item2Study
Item3Study
Item4Study
List<StudentClass>
BibleReading
Item1Student
Item1Assistant
Item2Student
Item2Assistant
Item3Student
Item3Assistant
Item4Student
Item4Assistant
以上是由以下内容构建的:
StudentClasses
NumClasses: # Classes
Item1Desc: Apply1_Description
Item2Desc: Apply2_Description
Item3Desc: Apply3_Description
Item4Desc: Apply4_Description
BibleReadingStudy: BibleReading_Study_A
Item1Study: Apply1_Study_A
Item2Study: Apply2_Study_A
Item3Study: Apply3_Study_A
Item4Study: Apply4_Study_A
List<StudentClass>
BibleReading BibleReading_A|B|C
Item1Student Apply1_A|B|C
Item1Assistant Apply1_Asst_A|B|C
Item2Student Apply2_A|B|C
Item2Assistant Apply2_Asst_A|B|C
Item3Student Apply3_A|B|C
Item3Assistant Apply3_Asst_A|B|C
Item4Student Apply4_A|B|C
Item5Assistant Apply4_Asst_A|B|C
现在您知道了字段与建议对象之间的映射。显然,我可以自己创建该 C# 类,并且可以手动将字段转换为这样的结构。但是CvsReader有没有像这样直接读入类的能力呢?而不是一大堆属性?