How to scroll through an enumerable list and check if the property repeats in the list during foreach turns

Asked

Viewed 319 times

3

I have a list (IEnumerable) called People that contains the properties Id, Datanasc, etc... I need to bring this data to a listview in the html using the Razor (or it can be outside the cshtml page, in the . Cs files).

Let’s say person one has

Id 1, Date 23/03/2000;

And person 2 (displayed on the second lap of the foreach in the view) has:

Id 2, and Datanasc 23/03/2000;

Datanasc being equal in person 1 and 2...

I would like to make a consultation of the type: "if the value of the Datanasc property on the People list is the same for several people, return all those people who were born that same day".

Can someone help me?

I’m using C# Asp.Net MVC 4 with Razor View.

I’m new at C#, I was trying to do this, which stopping to think I think doesn’t make much sense, but I’m putting it to maybe try to illustrate what I wanted, although with the wrong logic.

@foreach (var item in Model.Pessoas)
{
    for (int i = 0; i < Model.Pessoas.Count(); i++)
    {
        var arr = Model.Pessoas.ToArray();
        if (item.DataNasc == arr[i].DataNasc)
        {
            <p>@arr[i].DataNasc</p>
        }
    }
}
  • if you have a list with these 5 dates: 23/03/2000; 23/03/2000; 30/01/2002; 10/02/2001;10/02/2001... what would be the intended output?

  • You want to make a grouping?

2 answers

0

Good evening, have you ever thought of using a dictionary Dictionary<>? As a key you can use a DateTime and as value a list of class persons List<Pessoa>.

Example:

Dictionary<DateTime, List<Pessoa>> comMesmaData;
// Para checar se já há pessoas com esta data ou inserir novas!!!
if (comMesmaData.Conteins(DATA))
    comMesmaData[DATA].Add(NOVA_PESSOA);
else
    comMesmaData.Add(DATA, NOVA_PESSOA);

At the end you will have all the data of the people grouped by date.

  • hello! thanks for the reply! I am using an enumerable list, so I understood does not roll with it

0

I don’t quite understand what you mean by this, but you yourself have noticed this as you put it in your question. I say this because you may have N repeating groups. For example:

Id 1, Date 23/03/2000;

Id 1, Date NASC 11/07/1990;

Id 1, Date 23/03/2000;

Id 1, Date NASC 11/07/1990;

Id 1, Date 23/03/2000;

Id 1, Datanasc 01/01/1989;

Here you have a group of three people who were born in 23/03/2000 and another group with two people who were born in 11/07/1990. Which of the two repetitions would you list?

Here are some ways to work the repeated values you find in the explanations below.


Checking for repeated values in a list

I have a method I usually use to check repeated items in a list of string:

public static IEnumerable<string> VerificarItensRepetidos(IEnumerable<string> listString)
{
    List<string> duplicateKeys = new List<string>();
    List<string> notDuplicateKeys = new List<string>();
    foreach (var text in listString)
    {
        if (notDuplicateKeys.Contains(text))
        {
            duplicateKeys.Add(text);
        }
        else
        {
            notDuplicateKeys.Add(text);
        }
    }
    return duplicateKeys;
}

In your case you want to check a Date, then I made an improvement in the method in the explanation below for him to compare any type.


Comparing any kind

With the example below any type can be checked, rather than just string as in my previous example:

public static IEnumerable<T> VerificarItensRepetidos<T>(IEnumerable<T> listString)
{
    List<object> duplicateKeys = new List<object>();
    List<object> notDuplicateKeys = new List<object>();
    foreach (var text in listString)
    {
        if (notDuplicateKeys.Contains(text))
        {
            duplicateKeys.Add(text);
        }
        else
        {
            notDuplicateKeys.Add(text);
        }
    }
    return duplicateKeys.Cast<T>();
}

The method returns the list with the repeated items. See an example of use:

var listaRespetidos = Helper.VerificarItensRepetidos(minhaListaString);
if(listaRespetidos.Count() > 0)
{
    //faça aqui as tratativas necessárias...
}

How you want to work items that have the same date of birth, maybe in your case go through each returned item (foreach) and perform a search on your list for each of these groups separately.


See an example in Dot Net fiddle applied to your problem using the above method:

Dot Net fiddle

Browser other questions tagged

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