C# 解析(转换/映射)前过滤LinqToExcel中的空行

C# 解析(转换/映射)前过滤LinqToExcel中的空行,c#,.net,excel,transformation,linq-to-excel,C#,.net,Excel,Transformation,Linq To Excel,我正在使用LinqToExcel将Excel行映射到C#/.NET项目中的对象 我在转换函数中添加了验证代码,这样它们不仅可以转换数据,还可以在丢失某些数据时向用户发出警告。 例如: excel.AddTransformation(x=>x.PaymentPeriod,cellvalue=> { 如果(cellvalue.Length==0) { 抛出新异常(String.Format(Errors.EmptyField、ColumnNames.PaymentPeriod、ColumnNames

我正在使用LinqToExcel将Excel行映射到C#/.NET项目中的对象

我在转换函数中添加了验证代码,这样它们不仅可以转换数据,还可以在丢失某些数据时向用户发出警告。 例如:

excel.AddTransformation(x=>x.PaymentPeriod,cellvalue=>
{
如果(cellvalue.Length==0)
{
抛出新异常(String.Format(Errors.EmptyField、ColumnNames.PaymentPeriod、ColumnNames.EmployeeNumber、lastCheckedEmployeeNumber));
}
返回CultureInfo.InvariantCulture.TextInfo.ToTitleCase(cellvalue);
});
但是,我不希望此验证由Excel有时在底部添加的空行触发(请参阅)

我的问题是,我不能使用上面提到的解决方案,因为在调用类似

excel.Worksheet<SomeType>("WorksheetName").Where(row => row.Any(cell => cell != null));
excel.Worksheet(“工作表名称”)。其中(行=>row.Any(单元格=>cell!=null));
这是因为首先应用转换,然后对转换结果应用Where方法

另外-在转换函数中,我无法访问行中的其他值,因此无法检查它是单个空单元格(错误)还是行完全为空


是否可以在应用转换之前过滤掉空行?

是否有某些单元格仅在整行为空时才为空

例如,通常有一个Id列,除了空行之外,它总是填充。如果是这样,那么下面的查询应该适合您

//assuming Id cell is only blank when the whole row is blank
excel.WorkSheet<PaymentObject>().Where(x => x.Id != "");

//the Id cell might be null instead of blank, so use this Where clause instead
excel.WorkSheet<PaymentObject>().Where(x => x.Id != null);
//假设整行为空时Id单元格仅为空
excel.WorkSheet()。其中(x=>x.Id!=“”);
//Id单元格可能是null而不是空的,因此请改用Where子句
excel.WorkSheet()。其中(x=>x.Id!=null);

您可以将强类型工作表与非类型工作表合并,然后使用非类型工作表查找全部空行:

List<T> onlyNonBlankRows = _queryFactory.Worksheet<T>(firstWorksheetWithColumnHeaders)
    // calling ToList here is workaround to avoid Remotion.Data.Linq.Parsing.ParserException for Select((item,index) => ...) - "This overload of the method 'System.Linq.Queryable.Select' is currently not supported, but you can register your own parser if needed."
    .ToList()
    .Select((typedRow, index) => new { typedRow, index })
    // Join the worksheet to an untyped projection of the same worksheet so that we can find totally blank rows
    .Join(
        _queryFactory.Worksheet(firstWorksheetWithColumnHeaders)
    // calling ToList here is workaround to avoid Remotion.Data.Linq.Parsing.ParserException for Select((item,index) => ...)
                    .ToList()
                    .Select(
                        (untypedRow, indexForUntypedRow) =>
                        new { untypedRow, indexForUntypedRow }),
    // join on row index - row 1 matches row 1 etc
        arg => arg.index, arg => arg.indexForUntypedRow,
        (a, b) => new { a.index, a.typedRow, b.untypedRow })
    // Exclude rows where all cells are empty 
    .Where(x => x.untypedRow.Any(cell => cell.Value != DBNull.Value))
    .Select(joined => joined.typedRow).ToList();
List onlyNonBlankRows=\u queryFactory.Worksheet(FirstWorksheetWithColumnHeader)
//在此处调用ToList是避免Select((项,索引)=>…)的Remotion.Data.Linq.Parsing.ParserException的一种解决方法—“当前不支持此方法‘System.Linq.Queryable.Select’的重载,但如果需要,您可以注册自己的解析器。”
托利斯先生()
.Select((typedRow,index)=>new{typedRow,index})
//将工作表连接到同一工作表的非类型投影,以便我们可以找到完全空白的行
.加入(
_queryFactory.Worksheet(第一个工作表和列标题)
//在此处调用ToList是避免Select((项,索引)=>…)的Remotion.Data.Linq.Parsing.ParserException的解决方法
托利斯先生()
.选择(
(非TypedRow、indexForUntypedRow)=>
新的{untypedRow,indexForUntypedRow}),
//行索引上的联接-行1匹配行1等
arg=>arg.index,arg=>arg.indexForUntypedRow,
(a,b)=>新的{a.index,a.typedRow,b.untypedRow})
//排除所有单元格都为空的行
.Where(x=>x.untypedRow.Any(cell=>cell.Value!=DBNull.Value))
.Select(joined=>joined.typedRow.ToList();

另一个原因是
excel.Worksheet(“工作表名称”)。其中(row=>row.Any(cell=>cell!=null))不起作用的原因是LinqToExcel不支持子查询(异常消息:VisitSubQueryExpression方法未实现)。我考虑过这个选项,但忘记填充单个单元格可能是用户的错误。我想筛选完全为空的行,而转换方法无法做到这一点,因为它们只包含有关正在转换的单元格的信息。然后唯一的方法是包含一个Where子句,以确认每个单元格都为空。问题是,转换是在调用Where子句之前完成的。在应用转换之前,向LinqToExcel添加一个过滤器对其进行过滤可能会很有用?where过滤器应该在转换之前完成。LinqToExcel将where子句添加到从电子表格检索数据的sql语句中。但是,如果在Where子句之前调用ToList(),那么您是正确的,转换是在Where子句之前完成的。
List<T> onlyNonBlankRows = _queryFactory.Worksheet<T>(firstWorksheetWithColumnHeaders)
    // calling ToList here is workaround to avoid Remotion.Data.Linq.Parsing.ParserException for Select((item,index) => ...) - "This overload of the method 'System.Linq.Queryable.Select' is currently not supported, but you can register your own parser if needed."
    .ToList()
    .Select((typedRow, index) => new { typedRow, index })
    // Join the worksheet to an untyped projection of the same worksheet so that we can find totally blank rows
    .Join(
        _queryFactory.Worksheet(firstWorksheetWithColumnHeaders)
    // calling ToList here is workaround to avoid Remotion.Data.Linq.Parsing.ParserException for Select((item,index) => ...)
                    .ToList()
                    .Select(
                        (untypedRow, indexForUntypedRow) =>
                        new { untypedRow, indexForUntypedRow }),
    // join on row index - row 1 matches row 1 etc
        arg => arg.index, arg => arg.indexForUntypedRow,
        (a, b) => new { a.index, a.typedRow, b.untypedRow })
    // Exclude rows where all cells are empty 
    .Where(x => x.untypedRow.Any(cell => cell.Value != DBNull.Value))
    .Select(joined => joined.typedRow).ToList();