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". How can I get the result in join class? Below is the whole function to get called.
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; }
}
dont set as nullable. - if not found - object would be null, the way you join - it would be like inner join this is example (please, let me know if it fixed your issue)
void Main()
{
var jobModels = new List<JobModel>() { new JobModel() {
Id = "1",
Rig = "1",
Job_code = "000", Network = "www", Product_line = "sale", Created = DateTime.Now.ToString()
},
new JobModel() {
Id = "2",
Rig = "3",
Job_code = "000", Network = "www", Product_line = "sale", Created = DateTime.Now.ToString()
}};
var rigModels = new List<RigModel>() { new RigModel() { Rig = new Rigdetails() {
Id = "1" ,Title="rig1", Enabled= true
} }};
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 Rigdetails { Title = r.Rig.Title, Enabled = r.Rig.Enabled, Id = r.Rig.Id }
});
result.Dump();
}
public class RigJobBsonModel
{
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 Simple_help { get; set; }
public string Created { get; set; }
public string Modified { get; set; }
public string Modified_by { get; set; }
public Rigdetails Rigs { get; set; }
}
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; }
}
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; }
}
there would be only 1 result which is matching (Rig 3 in example is not exists so it will not link)
if you need to show other RigJobBsonModel you need a different technique to join tables.