如何将 Linq 更改为 excel 空字符串值?

How to set change Linq to excel Null string values?

我有一个模型可以使用 LINQ 将对象列表转换为 excel。

public class Model{
  public string Name { get; set; }
  public string Date { get; set; }
}

我正在使用

var result = excelQueryFactory.Warksheet<Model>(0);

但是我的 excel 在名称单元格中进行了 Null 测试。但它们应该是空的。所以我的 Name 属性充满了 Null 文本。如何在填充模型时 excel 这些文本值?

也许这种非常常见的模式会满足您的要求。

public class Model {

    private string _name;

    public string Name { 

        get => _name; 

        set {
            _name = (value == null_value) ? empty_value : value;
        }
    } 
}
public class Model
{
    private string _Name;

    public string Name
    {
        get { return _Name; }
        set
        {
            if (string.IsNullOrWhiteSpace(value) || value.ToLower() == "null")
                _Name = null;
            else
                _Name = value;
        }
    }

    public string Date { get; set; }
}

您是否尝试过使用 LINQ 中的 SELECT 选项将空值转换为字符串?

var result = excelQueryFactory.Warksheet<Model>(0)
    .Select(x => new Model{
        Name = x.Name ?? string.Empty,
        Date = x.Date
    });

您可以向 excel 工厂对象添加以下转换:

excelQueryFactory.AddTransformation<Model>(x => x.Date, cellValue => cellValue??string.Empty);
excelQueryFactory.AddTransformation<Model>(x => x.Name, cellValue => cellValue??string.Empty);