You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0 to create an application.
The application connects to a Microsoft SQL Server database.
You use the following SQL statement to retrieve an instance of a DataSet object named ds:
SELECT CustomerID, CompanyName, ContactName, Address, City
FROM dbo.Customers
You need to query the DataSet object to retrieve only the rows where the ContactName field is not NULL. Which code segment should you use?
A.
from row in ds.Tables[0].AsEnumerable()
where (string)row[“ContactName”] != null
select row;
B.
from row in ds.Tables[0].AsEnumerable()
where row.Field<string>(“ContactName”) != null
select row;
C.
from row in ds.Tables[0].AsEnumerable()
where !row.IsNull((string)row[“ContactName”])
select row;
D.
from row in ds.Tables[0].AsEnumerable()
where !Convert.IsDBNull(row.Field<string>(“ContactName”))
select row;
Explanation:
Field<T>(DataRow, String) Provides strongly-typed access to each of the column values in the specified row.
The Field method also supports nullable types.
Just to explain why the correct answer is not A. Calling row[“ContactName”] will not return ‘null’ for ‘null’ values. It will return ‘DBNull.Value’ instead.
See more detailed explanation here:
http://stackoverflow.com/questions/9517682/dataset-comparison-with-null-value-c-sharp
On the other hand row.Field does return NULL for empty fields, which is why the specified answer is correct