0

我有一个 Linq 查询,如下所示

var ResultQuery = from Task in dtTasks.AsEnumerable()
                              select new { EmployeeName = Task.Field<string>("EmpName").Split(':')[1],
                                           EmployeeCode = Task.Field<string>("EmpName").Split(':')[0],
                                           ProjectId = Task.Field<string>("ProjectName").Split(':')[0],
                                           ProjectName = Task.Field<string>("ProjectName").Split(':')[1],
                                           TaskName = Task.Field<string>("TaskName"),
                                           TaskDescription = Task.Field<string>("TaskDescription"),
                                           StartDate = Task.Field<DateTime>("StartDate"),
                                           Duration = Task.Field<double>("Duration"),
                                           EndDate = Task.Field<DateTime>("EndDate"),
                                           LeadName = Task.Field<string>("LeadBy").Split(':')[1],
                                           LeadId = Task.Field<string>("LeadBy").Split(':')[0]
                              };

foreach (var task in ResultQuery)
            {

            }

dtTasks是从 Microsoft Excel 文件导入的表格。由于它是一个用户输入文件,其中可能有很多错别字。String.Split在 Linq Query 中,如果任何 DateTime 转换失败,或者对象引用未设置为实例错误(插入空白行或空行时的方法),我该如何处理?我想跳过这些迭代并向用户发出关于跳过原因的警告。如果我为此使用 Linq,我该如何实现?

4

1 回答 1

0

一旦出现异常,枚举就会被破坏——你不能进一步迭代。

以下是一种解决方法:

var ResultQuery = dtTasks.AsEnumerable()
                  .Select(Task => {
                              try {  
                                  return new { EmployeeName = Task.Field<string>("EmpName").Split(':')[1],
                                           EmployeeCode = Task.Field<string>("EmpName").Split(':')[0],
                                           ProjectId = Task.Field<string>("ProjectName").Split(':')[0],
                                           ProjectName = Task.Field<string>("ProjectName").Split(':')[1],
                                           TaskName = Task.Field<string>("TaskName"),
                                           TaskDescription = Task.Field<string>("TaskDescription"),
                                           StartDate = Task.Field<DateTime>("StartDate"),
                                           Duration = Task.Field<double>("Duration"),
                                           EndDate = Task.Field<DateTime>("EndDate"),
                                           LeadName = Task.Field<string>("LeadBy").Split(':')[1],
                                           LeadId = Task.Field<string>("LeadBy").Split(':')[0]
                                  }
                               } catch(Exception e) {
                                        Console.WriteLine("Print your warning!");
                                        return null;
                               }
                  });



foreach (var task in ResultQuery)
{
   if(task != null) {
        // do your processing
   }
}
于 2013-10-16T05:53:36.477 回答