C# 检查列表 <T> 是否为 null
C# Check List<T> for null
我有一个 List<T>
如下所示,列表可以包含空值,下面只是一个例子。
contactEntries = new List<ContactEntries>();
contactEntries.Add(new ContactEntries() { isPrimary = true, contactPerson = ceo, telephone = telephone, telephone1 = telephone1, address = address, postalCode = postalCode, city = city, email = email, role = (Roles)role, contry = (Countries)country });
这里是 class
public class ContactEntries
{
[DisplayName("Primary:")]
public bool isPrimary { get; set; }
[DisplayName("Contact person:")]
public string contactPerson { get; set; }
[DisplayName("Telephone:")]
public string telephone { get; set; }
[DisplayName("Telephone 1:")]
public string telephone1 { get; set; }
[DisplayName("Address:")]
public string address { get; set; }
[DisplayName("Postal code:")]
public string postalCode { get; set; }
[DisplayName("City:")]
public string city { get; set; }
[DisplayName("Email:")]
public string email { get; set; }
[DisplayName("Role:")]
public Roles role { get; set; }
[DisplayName("Country:")]
public Countries contry { get; set; }
}
public enum Roles
{
CEO = 0,
IT = 1
}
public enum Countries
{
Denmark = 0,
Norway = 1,
Sweden = 2
}
下面是我需要检查空值的地方。请注意,这是一个表单,class (_contactEntries) 在另一个 class 中,因此存在一些可访问性问题
private void WizardCreateOrder_Finish(object sender, EventArgs e)
{
// Add client
MySQL.Clients.Insert.Client(_orgNo, _companyName, _ceo, _companyType, _country, _leadId, _countryCode, out int clientId);
// Here I need to check for null
// Add contact persons
foreach (var entry in _contactEntries)
{
MySQL.Clients.ContactPerson.Insert.ContactPerson(entry.contactPerson, entry.telephone, entry.telephone1, entry.address, entry.postalCode,
entry.city, entry.email, _country, _countryCode, entry.role.ToString(),
entry.isPrimary.ToString(), DateTime.Now, clientId, _leadId);
}
}
我的问题是:如何检查列表中的所有这些元素是否为 null,而不必逐一检查它们,例如使用 linq 输出一个布尔值来判断是否存在是否为空值。
编写一个检查单个对象是否为空值的方法,然后使用 LINQ 检查所有元素。
bool ContainsNull(ContactEntries item)
{
return item == null
|| item.contactPerson == null
|| item.telephone == null
|| item.telephone1 == null
|| item.address == null
|| item.postalCode == null
|| item.city == null
|| item.email == null
|| item.roles == null
|| item.contry == null; //sic
}
然后查看列表:
bool hasNulls = contactList.Any(ContainsNull);
您可以使用反射来实现。反射是一种 C# 功能,它允许代码在运行时访问自身以在运行时设置值、读取属性或动态创建属性的事件,但请注意反射可能非常慢,具体取决于您正在做什么。
请记住,只有 nullable<T>
、strings
和 objects
等引用类型可以为 null,值类型 like
、bool
、int
, double
,等都有默认值
public bool CheckAnyNull(ContactEntries contactEntries)
{
// Get all properties of the class an iterates
foreach (var item in contactEntries.GetType().GetProperties())
{
// Check if property is null
if (item.GetValue(contactEntries) == null)
return true;
}
return false;
}
我有一个 List<T>
如下所示,列表可以包含空值,下面只是一个例子。
contactEntries = new List<ContactEntries>();
contactEntries.Add(new ContactEntries() { isPrimary = true, contactPerson = ceo, telephone = telephone, telephone1 = telephone1, address = address, postalCode = postalCode, city = city, email = email, role = (Roles)role, contry = (Countries)country });
这里是 class
public class ContactEntries
{
[DisplayName("Primary:")]
public bool isPrimary { get; set; }
[DisplayName("Contact person:")]
public string contactPerson { get; set; }
[DisplayName("Telephone:")]
public string telephone { get; set; }
[DisplayName("Telephone 1:")]
public string telephone1 { get; set; }
[DisplayName("Address:")]
public string address { get; set; }
[DisplayName("Postal code:")]
public string postalCode { get; set; }
[DisplayName("City:")]
public string city { get; set; }
[DisplayName("Email:")]
public string email { get; set; }
[DisplayName("Role:")]
public Roles role { get; set; }
[DisplayName("Country:")]
public Countries contry { get; set; }
}
public enum Roles
{
CEO = 0,
IT = 1
}
public enum Countries
{
Denmark = 0,
Norway = 1,
Sweden = 2
}
下面是我需要检查空值的地方。请注意,这是一个表单,class (_contactEntries) 在另一个 class 中,因此存在一些可访问性问题
private void WizardCreateOrder_Finish(object sender, EventArgs e)
{
// Add client
MySQL.Clients.Insert.Client(_orgNo, _companyName, _ceo, _companyType, _country, _leadId, _countryCode, out int clientId);
// Here I need to check for null
// Add contact persons
foreach (var entry in _contactEntries)
{
MySQL.Clients.ContactPerson.Insert.ContactPerson(entry.contactPerson, entry.telephone, entry.telephone1, entry.address, entry.postalCode,
entry.city, entry.email, _country, _countryCode, entry.role.ToString(),
entry.isPrimary.ToString(), DateTime.Now, clientId, _leadId);
}
}
我的问题是:如何检查列表中的所有这些元素是否为 null,而不必逐一检查它们,例如使用 linq 输出一个布尔值来判断是否存在是否为空值。
编写一个检查单个对象是否为空值的方法,然后使用 LINQ 检查所有元素。
bool ContainsNull(ContactEntries item)
{
return item == null
|| item.contactPerson == null
|| item.telephone == null
|| item.telephone1 == null
|| item.address == null
|| item.postalCode == null
|| item.city == null
|| item.email == null
|| item.roles == null
|| item.contry == null; //sic
}
然后查看列表:
bool hasNulls = contactList.Any(ContainsNull);
您可以使用反射来实现。反射是一种 C# 功能,它允许代码在运行时访问自身以在运行时设置值、读取属性或动态创建属性的事件,但请注意反射可能非常慢,具体取决于您正在做什么。
请记住,只有 nullable<T>
、strings
和 objects
等引用类型可以为 null,值类型 like
、bool
、int
, double
,等都有默认值
public bool CheckAnyNull(ContactEntries contactEntries)
{
// Get all properties of the class an iterates
foreach (var item in contactEntries.GetType().GetProperties())
{
// Check if property is null
if (item.GetValue(contactEntries) == null)
return true;
}
return false;
}