0

I have made class in c#. after I had been a list consists from this class. the class has attribute groupId (The elements do not follow each other). I want to get the elements by the groupId. this is the class:

class GroupListItem
{
    public GroupListItem(List<string> placeIdString)
    {
        GroupId = id++;
        PlaceIdString = placeIdString;
    }
    private static int id=0;
    public int GroupId { get; set; }
    public List<String> PlaceIdString { get; set; }
}

this is the list declaration:

List<GroupListItem> groupListItems = new List<GroupListItem>();
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Ayala
  • 11
  • 3
  • `I want to get the elements by the groupId` ← Do you mean you want to filter the existing `List` instance by an ID? There are many examples of how to do this on [so] and other sites. You can use loops (pick one and try it) or Linq / Lambdas which abstracts the loops for you. – Igor Jan 31 '22 at 13:31
  • var resuts = groupListItems.Where(x => x.id == 1234).Select(x => x.PlaceIdString).ToList(); – jdweng Jan 31 '22 at 13:35

1 Answers1

1

you can do:

int myValue....
.... groupListItems.Where(i => i.GroupId == myValue).FirstOrDefault();

the where doc:

https://learn.microsoft.com/de-de/dotnet/api/system.linq.enumerable.where?view=net-6.0

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97