I have a class Rigmodel, inside class RigModel I have RigDetails class as a properties. I initialize RigDetails in constructor of RigModel as below.
public class RigModel
{
public RigModel()
{
Rig = new Rigdetails();
}
public Rigdetails Rig { get; set; }
}
public class Rigdetails
{
public string Id { get; set; }
public string Title { get; set; }
public bool Enabled { get; set; }
}
I have another class JobModel.
public class JobModel
{
public string Id { get; set; }
public string Job_code { get; set; }
public string Product_line { get; set; }
public string Geomarket { get; set; }
public string Roc { get; set; }
public string Network { get; set; }
public string Rig { get; set; }
public string Simple_help { get; set; }
public string Created { get; set; }
public string Modified { get; set; }
public string Modified_by { get; set; }
}
I got two seperate list jobmodels and rigmodels with above two classes. now binding both the list properties into new model(JobRigBsonModel). Using below query for this.
var result = jobModels.Join(rigModels, j => j.Rig, r => **r.Rig.Id**, (j, r) =>
new RigJobBsonModel
{
Id = j.Id,
Job_code = j.Job_code,
Product_line = j.Product_line,
Geomarket = j.Geomarket,
Roc = j.Roc,
Network = j.Network,
Simple_help = j.Simple_help,
Created = j.Created,
Modified = j.Modified,
Modified_by = j.Modified_by,
Rigs = new Rig { Title = r.Rig.Title, Enabled = r.Rig.Enabled }
});
Getting error as bold in above query as "object reference not set to an instance of an object". Can anyone guide me how can I get the result in join class. Below is the whole function to get called. Its urgent please anyone guide.
private static List<RigJobBsonModel> updateJobWithRigDetails(
List<JobModel> jobModels, List<RigModel> rigModels)
{
try
{
var result = jobModels.Join(rigModels, j => j.Rig, r => r.Rig.Id, (j, r) =>
new RigJobBsonModel
{
Id = j.Id,
Job_code = j.Job_code,
Product_line = j.Product_line,
Geomarket = j.Geomarket,
Roc = j.Roc,
Network = j.Network,
Simple_help = j.Simple_help,
Created = j.Created,
Modified = j.Modified,
Modified_by = j.Modified_by,
Rigs = new Rig { Title = r.Rig.Title, Enabled = r.Rig.Enabled }
});
return result.ToList();
}
catch (System.Exception ex)
{
throw ex;
}
}
Below is the existing Model, which i am binding while query with above linq query.
public class RigJobBsonModel
{
public string Id { get; set; }
[BsonElement]
public string Job_code { get; set; }
[BsonElement]
public string Product_line { get; set; }
[BsonElement]
public string Geomarket { get; set; }
[BsonElement]
public string Roc { get; set; }
[BsonElement]
public string Network { get; set; }
[BsonElement]
public string Simple_help { get; set; }
[BsonElement]
public string Created { get; set; }
[BsonElement]
public string Modified { get; set; }
[BsonElement]
public string Modified_by { get; set; }
#nullable enable
[BsonElement]
public RigDetails? Rigs { get; set; }
}
Please help..