Comparison of Object Lists - C#

Asked

Viewed 564 times

2

I need to compare 2 lists of objects that were populated via database, as classes below:

public class Ausente
{
    public ObjectId Id { get; set; }
    public Int32 RA { get; set; }
}

public class AlunoEstagio
{
    public ObjectId Id { get; set; }
    public Int32 RA { get; set; }
    public String ALUNO { get; set; }
    public Int32 PERIODO { get; set; }
    public String DISCIPLINA { get; set; }
    public char CONCEITO { get; set; }
}

List 1 will receive the Missing object, list 2 will receive the Student Stage object.

I would like to compare the 2 lists so that when there is a List 1 RA attribute present in List 2, the corresponding List 2 object is added in a third list (List 3).

I have no idea how to make this comparison, could help me?

  • What have you done? What problems have you encountered? Show some code, at least try to do something so we can help with your difficulty.

1 answer

0


If the two guys were the same, you could use the Intersect, but as they are not, I think the code below does what you need:

var list3 = from l1 in list1
            join l2 in list2 on l1.RA equals l2.RA
            select new
            {
                l1,
                l2
            };

Where list1 and list2 are of the type List<Ausente> and List<AlunoEstagio> respectively.

  • Thanks Bruno, but this list3 variable, should I convert to an attribute of the right List type? The idea is to display the content in a Datagridview

  • Just change the select by selecting what you need to show on the grid. If you tell me what fields you need, I’ll change the answer for you.

  • And depending on the control you use, you don’t need to convert to list or change the select, in the fields you can change the bindind to something like L1.RA, L2.STUDENT etc...

  • Thanks Bruno, I already got, I just added a . Tolist() between the last key and the semicolon and funfou ! Helped me very expensive, thanks

Browser other questions tagged

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