segment should you use?

You create an application by using the Microsoft .NET Framework 3.5 and Microsoft
ADO.NET.The application uses the following LINQ query.
var query = from o in orderLinesQuery
where (string)o[“CarrierTrackingNumber”] == “AEB6-4356-80”
select new
{
SalesOrderID = o.Field<int>(“SalesOrderID”),
OrderDate = o.Field<DateTime>(“OrderDate”)
};
The CarrierTrackingNumber field in the DataRow is nullable.You need to ensure that an
exception does not occur if the CarrierTrackingNumber field has a null value.Which code
segment should you use?

You create an application by using the Microsoft .NET Framework 3.5 and Microsoft
ADO.NET.The application uses the following LINQ query.
var query = from o in orderLinesQuery
where (string)o[“CarrierTrackingNumber”] == “AEB6-4356-80”
select new
{
SalesOrderID = o.Field<int>(“SalesOrderID”),
OrderDate = o.Field<DateTime>(“OrderDate”)
};
The CarrierTrackingNumber field in the DataRow is nullable.You need to ensure that an
exception does not occur if the CarrierTrackingNumber field has a null value.Which code
segment should you use?

A.
var query = from o in orderLinesQuery
where !o.IsNull(“CarrierTrackingNumber”) &&
(string)o[“CarrierTrackingNumber”] == “AEB6-4356-80”
select new
{
SalesOrderID = o.Field<int>(“SalesOrderID”),
OrderDate =o.Field<DateTime>(“OrderDate”)
};

B.
var query = from o in orderLinesQuery
where o.IsNull(“CarrierTrackingNumber”) &&
(string)o[“CarrierTrackingNumber”] == “AEB6-4356-80”
select new
{
SalesOrderID = o.Field<int>(“SalesOrderID”),
OrderDate =o.Field<DateTime>(“OrderDate”)
};

C.
var query = from o in orderLinesQuery
where o.Field<string>(“CarrierTrackingNumber”) == “AEB6-4356-80”
select new
{
SalesOrderID = o.Field<int>(“SalesOrderID”),
OrderDate = o.Field<DateTime>(“OrderDate”)
};

D.
var query = from o in orderLinesQuery
where (string)o[“CarrierTrackingNumber”] == DBNull.Value &&
(string)o[“CarrierTrackingNumber”] == “AEB6-4356-80”
select new
{
SalesOrderID = o.Field<int>(“SalesOrderID”),
OrderDate =o.Field<DateTime>(“OrderDate”)
};

Explanation:
Here is some sample code that was used to test the above LINQ query:

DataTable dt = new DataTable(“Sales”);
dt.Columns.Add(“SalesOrderID”, typeof(Int32));
dt.Columns.Add(“OrderDate”, typeof(DateTime));
dt.Columns.Add(“CarrierTrackingNumber”, typeof(String));
dt.Rows.Add(1, “02/12/2009”, “MT297EM”);
dt.Rows.Add(2, “07/03/2010”, “MT33SG”);
dt.Rows.Add(3, “04/30/2010”, “MT73229MP”);
dt.Rows.Add(4, “06/01/2010”, “AEB6-4356-80”);
dt.Rows.Add(5, “10/19/2010”, DBNull.Value);
var orderLinesQuery = dt.AsEnumerable();

1. Answer choice A and C both have valid syntax and essentially return the exact same record.
2. Answer choice B will cause an Exception to be thrown since it will return the NULL record.
3. Answer choice D causes a syntax error since DBNull.Value cannot be cast to a string. In order to compare a string to DBNull.Value, it must be expressed in this manner: DBNull.Value.ToString()



Leave a Reply 0

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