Which code segment should you use?

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?

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.



Leave a Reply 2

Your email address will not be published. Required fields are marked *


John Galt

John Galt

On the other hand row.Field does return NULL for empty fields, which is why the specified answer is correct