Return 空字符串而不是 Null
Return Empty String Instead of Null
我正在使用 ef core 和 mapster。我的数据库中有一些可以为空的列。
当我从数据库中获取它们时,C# 将它们存储为空值(这是有道理的)。我想 return 这些字段是空字符串,尽管当我通过 api.
将它们发回时
public class CompanyDto
{
public string Website { get; set; }
}
public class Company
{
public int Id { get; set; }
public string Website { get; set; } = "";
}
company.Adapt<CompanyDto>()
使 CompanyDto 中的 Website 为空字符串的最佳方法是什么。
经典 setter 也能胜任
public class Company
{
public int Id { get; set; }
private string _website;
public string Website
{
get { return _website; }
set { _website = value ?? string.Empty; }
};
public Company ()
{
_website = string.empty;
}
}
我正在使用 ef core 和 mapster。我的数据库中有一些可以为空的列。
当我从数据库中获取它们时,C# 将它们存储为空值(这是有道理的)。我想 return 这些字段是空字符串,尽管当我通过 api.
将它们发回时public class CompanyDto
{
public string Website { get; set; }
}
public class Company
{
public int Id { get; set; }
public string Website { get; set; } = "";
}
company.Adapt<CompanyDto>()
使 CompanyDto 中的 Website 为空字符串的最佳方法是什么。
经典 setter 也能胜任
public class Company
{
public int Id { get; set; }
private string _website;
public string Website
{
get { return _website; }
set { _website = value ?? string.Empty; }
};
public Company ()
{
_website = string.empty;
}
}