Crystal Report and LINQ
I am using Crystal Report for creating a report. I have two tables, Driver_Specifications
and WatchRow
.
Primary key of WatchRow
is foreign key of Driver_Specifications
. I set some field in my report file to show the data of Driver_Specifications
and I want to show a specific field of WatchRow
开发者_StackOverflow中文版instead of the foreign key of Driver_Specifications
. What is the query of LINQ for this problem?
This is what I have so far:
var q = (from d in taxi_AgencyDataSet.Driver_Specifications
orderby d.First_Name, d.ID_Driver
select new {
d.First_Name,
d.Last_Name,
d.Car_Name,
d.Car_Color,
d.WatchRow.Watch_Name,
d.ID_Watch
}).ToList();
I get the following error:
Object reference not set to an instance of an object
You need to check for null
when accessing d.WatchRow
to avoid throwing a NullReferenceException
:
var q = (from d in taxi_AgencyDataSet.Driver_Specifications
orderby d.First_Name, d.ID_Driver
select new
{
d.First_Name,
d.Last_Name,
d.Car_Name,
d.Car_Color,
Watch_Name = d.WatchRow == null ? "None" : d.WatchRow.Watch_Name,
d.ID_Watch
}).ToList();
精彩评论