1

I have a model to cast an object list using LINQ to excel.

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

and I am using

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

But my excel has Null test in name cells. But they should be empty. So my Name properties filled with Null text. How can I excel these text values while filling the model?

Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49
barteloma
  • 6,403
  • 14
  • 79
  • 173

4 Answers4

1

Perhaps this very common pattern will fit your requirements.

public class Model {

    private string _name;

    public string Name { 

        get => _name; 

        set {
            _name = (value == null_value) ? empty_value : value;
        }
    } 
}
Nicholas Hunter
  • 1,791
  • 1
  • 11
  • 14
1
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; }
}
Cem PEHLIVAN
  • 141
  • 3
0

Have you tried casting null values to string using the SELECT option in LINQ?

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

You could add following transformation to your excel factory object:

excelQueryFactory.AddTransformation<Model>(x => x.Date, cellValue => cellValue??string.Empty);
excelQueryFactory.AddTransformation<Model>(x => x.Name, cellValue => cellValue??string.Empty);
Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49