0

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..

  • is "Rigs=new Rig" should be a "Rigs = new Rigdetails {" (or there is it a List - not clear) also, i assume, RigJobBsonModel its an existing class.(you did not specified in question) (if you not set a class name there would be an anonymous object) – Power Mouse Jun 01 '21 at 15:23
  • @PowerMouse: Thanks for your suggestion, but no luck. I have used Rigs=new Rig because Rig is another bson class inside RigJobBsonModel as a property, which i need to bind with RigModel's child object. Yes RigJobBsonModel is the existing class. – IT Stretegist Jun 01 '21 at 15:36
  • please update your question to include missing info, in addition, in RigJobBsonModel check type of Rigs property – Power Mouse Jun 01 '21 at 15:50
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Jun 02 '21 at 06:24

2 Answers2

-1

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)

enter image description here

if you need to show other RigJobBsonModel you need a different technique to join tables.

Power Mouse
  • 727
  • 6
  • 16
-1

another approach (based on previos comment)

var lst = new List<RigJobBsonModel>();
    jobModels.ForEach(fe =>
    {
        var rj = new RigJobBsonModel()
        {
            Id = fe.Id,
            Job_code = fe.Job_code,
            Product_line = fe.Product_line,
            Geomarket = fe.Geomarket,
            Roc = fe.Roc,
            Network = fe.Network,
            Simple_help = fe.Simple_help,
            Created = fe.Created,
            Modified = fe.Modified,
            Modified_by = fe.Modified_by,
            Rigs = rigModels.Where(rm => rm.Rig.Id == fe.Rig).FirstOrDefault()?.Rig
        };
        lst.Add(rj);
    });
    lst.Dump();     

will return you

enter image description here

please pay attantion to:

.FirstOrDefault()?.Rig
Power Mouse
  • 727
  • 6
  • 16