如何以更简洁、更短和更高效的方式编写具有空检查的哨兵循环?

How to write this sentinel loop having a null check in a cleaner, shorter and efficient way?

我正在使用 null 作为 sentinel/trip 值来突破 foreach:

public static DataTable ExecuteQuery(string sql)
{
    var conn = new SqlConnection(EnvironmentVariables.GetDbConnectionString());
    var adapter = new SqlDataAdapter(sql, conn);
    var table = new DataTable();
    adapter.Fill(table);
    return table;
}
private VacantSummaryPage SelectPropertyFromDb(string sql)
{
    VacantProperty vacantProperty = null;
    List<VacantProperty> vacantProperties = GetProperties();
    foreach (DataRow row in SqlServer.ExecuteQuery(sql).Rows)
    {
        vacantProperty = vacantProperties.Find(v => v.GetTitle().Contains(row["PartialStreetAddress"].ToString()));
        if (vacantProperty != null) break;
    }
    vacantProperty.Select();
    return new VacantSummaryPage(driver);
}

它所做的是,一旦 vacantProperty 匹配到它不再为空,就跳出循环,Select() 即 属性.

您可以使用 LINQ 理解查询(SQL 类语法)编写“单行”指令:

private VacantSummaryPage SelectPropertyFromDb(string sql)
{
  var query = from row in SqlServer.ExecuteQuery(sql).Rows.Cast<DataRow>()
              from property in GetProperties()
              where property.GetTitle().Contains(row["PartialStreetAddress"].ToString())
              select property;
  vacantProperty = query.FirstOrDefault();
  if ( vacantProperty != null )
    vacantProperty.Select();
  return new VacantSummaryPage(driver);
}

如果未找到,仅使用 First 将引发异常,因此使用 FirstOrDefault 我们可以控制未找到的情况,除非首选异常控制。

此处查询本身被延迟,并且在调用 First/FirstOrDefault 或 ToList() 时对其进行评估。

Deferred Execution of LINQ Query

LINQ: Why is it called "Comprehension Syntax"

Query Syntax and Method Syntax in LINQ (C#)

LINQ Query Syntax (Comprehensive)

LINQ Method Syntax (Lambda)

很高兴知道

TypedTableBase used by DataSet allow to directly implements IEnumerable<DataRow> and so also Typed DataSets.