How to join two different lists using LINQ and Where?

Asked

Viewed 986 times

1

I have three classes:

class Cid
{
        public string cid  { get; set; } //importo esse campo via txt
        public string descricao { get; set; } //importo esse campo via txt
}

class Relacionamento
{
        public string cid { get; set; } //importo esse campo via txt
        public string codigo { get; set; } //importo esse campo via txt
        public string procedimento { get; set; } //PRECISO DESSE CAMPO
}

class Procedimento
{
        public string codigo { get; set; } //importo esse campo via txt
        public string procedimento { get; set; } //importo esse campo via txt
}

I created a list for the classes: listaCid to the classe Cid, and listaProcedimento to the classe Procedimento

After the created classes, the user clicks on an listaCid.

After clicking, the listaRelacionamento is filtered using Where Cid.cid = Relacionamento.Cid. So far so good.

I want to know how to include in listaRelacionamento (after the filter), the field Relacionamento.procedimento from listaProcedimento

inserir a descrição da imagem aqui

From the image, you can see: The user clicks on the top grid that receives the Cid list (in this case, received the A013 code) and switches to the bottom grid.Link (see that filters all fields where Cid = A013), but the third column is empty.

I want to fill the third column of the listRelationment by blue painted field (procedure)

  • 1

    Try to put also the procedure you say you are doing. It is very confusing what you intend to do. By the way, do not use the tag visual-studio if the problem is unrelated to the IDE. Read tip: What is a programming language, IDE and compiler?

  • I’ll change the question

  • Are all these classes collections? Come from where these collections?

  • I don’t know if they are collections. I created the classes as I showed in the code, by hand. and import the data from a txt file.

  • I don’t quite understand the class Relacionamento, what is the purpose of the field codigo and procedimento of this class?

  • It wouldn’t be easier just to create the fields codcid and codprocedimento in class Relacionamento? But anyway, this confusing your question, I suggest you be more specific and make it easy for us here.

  • The purpose of the relationship table is to join the Cid tables and procedure

  • the issue is that txt files with this information comes ready. it might even be easier to do the way you said above, but anyway I would have to put the files together to create the new fields in the Relationship class.

Show 3 more comments

1 answer

1


I think if I understand your question correctly what you want is this:

 List<Relacionamento> result = (from relacionamento in ListaRelacionamentos
                    join procedimento in ListaProcedimento
                         on relacionamento.codigo equals procedimento.codigo
                    select new Relacionamento()
                    {
                     cid =   relacionamento.cid,
                      codigo =  relacionamento.codigo,
                       procedimento = procedimento.procedimento
                    }).ToList();

But I recommend using Entity framework and objects ... for example: da would only have to navigate inside the object when doing the filter.

 class Relacionamento
 {
     public string cid { get; set; } //importo esse campo via txt
    public Procedimentos procedimento { get; set; } //importo esse campo via txt
}
  • can you give me an example of Entity framework and objects that you talked about? I think it will solve, but I still don’t know how to implement

  • your first tip worked but if you can explain the second tip better (put one class inside the other), I think it’s good to learn . hug.

  • 1

    I think this is a little complex to be explaining like this. but in short, any object can contain multiple objects, just like I put in the second example.. Being available for reading through the parent object. Investigate a bit about Entity framework, for simple projects.. basically install Entity and import/create the data model for the project.. From that moment on you could do for example: Relationamento rel = new Relationship(); .. and then to access the procedure.. rel.Procedimento.name;

  • but as I said, this can be very confusing.. Express a tutorial or something..

  • ok, anyway, your tip helped me solve my problem. hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.