I need list of ids from list of project and want to check it against project id in query

This is my project list, I need ids of project from this list and want to store it in list. Then I want to check the project id if it is in list in query. Can anyone guide?

[
  %Appeo.Leads.Project{
    __meta__: #Ecto.Schema.Metadata<:loaded, "projects">,
    activities: #Ecto.Association.NotLoaded<association :activities is not loaded>,
    appointments: #Ecto.Association.NotLoaded<association :appointments is not loaded>,
    buildings: #Ecto.Association.NotLoaded<association :buildings is not loaded>,
    comments: #Ecto.Association.NotLoaded<association :comments is not loaded>,
    company: #Ecto.Association.NotLoaded<association :company is not loaded>,
    company_contact: #Ecto.Association.NotLoaded<association :company_contact is not loaded>,
    company_contact_id: 12,
    company_id: 2,
    contacts: #Ecto.Association.NotLoaded<association :contacts is not loaded>,
    description: "Debitis quis eligend",
    estimated_time: 88,
    id: 14,
    index: 1.0,
    inserted_at: ~N[2022-06-10 09:37:45],
    lead_column: #Ecto.Association.NotLoaded<association :lead_column is not loaded>,
    lead_column_id: 6,
    name: "Bryar Flores",
    project_buildings: #Ecto.Association.NotLoaded<association :project_buildings is not loaded>,
    project_contacts: #Ecto.Association.NotLoaded<association :project_contacts is not loaded>,
    project_image: nil,
    project_materials: #Ecto.Association.NotLoaded<association :project_materials is not loaded>,
    project_permission_joined_users: #Ecto.Association.NotLoaded<association :project_permission_joined_users is not loaded>,
    project_stage: nil,
    project_start_date: ~U[1970-08-10 20:25:00Z],
    project_type: "roofing",
    status: :active,
    tasks: #Ecto.Association.NotLoaded<association :tasks is not loaded>,
    updated_at: ~N[2022-06-10 09:37:45]
  },
  %Appeo.Leads.Project{
    __meta__: #Ecto.Schema.Metadata<:loaded, "projects">,
    activities: #Ecto.Association.NotLoaded<association :activities is not loaded>,
    appointments: #Ecto.Association.NotLoaded<association :appointments is not loaded>,
    buildings: #Ecto.Association.NotLoaded<association :buildings is not loaded>,
    comments: #Ecto.Association.NotLoaded<association :comments is not loaded>,
    company: #Ecto.Association.NotLoaded<association :company is not loaded>,
    company_contact: #Ecto.Association.NotLoaded<association :company_contact is not loaded>,
    company_contact_id: 3,
    company_id: 2,
    contacts: #Ecto.Association.NotLoaded<association :contacts is not loaded>,
    description: "Ipsa vero dolore la",
    estimated_time: 85,
    id: 15,
    index: 1.0,
    inserted_at: ~N[2022-06-10 09:39:23],
    lead_column: #Ecto.Association.NotLoaded<association :lead_column is not loaded>,
    lead_column_id: 7,
    name: "Keelie Coffey",
    project_buildings: #Ecto.Association.NotLoaded<association :project_buildings is not loaded>,
    project_contacts: #Ecto.Association.NotLoaded<association :project_contacts is not loaded>,
    project_image: nil,
    project_materials: #Ecto.Association.NotLoaded<association :project_materials is not loaded>,
    project_permission_joined_users: #Ecto.Association.NotLoaded<association :project_permission_joined_users is not loaded>,
    project_stage: nil,
    project_start_date: ~U[1986-08-22 17:27:00Z],
    project_type: "any name",
    status: :active,
    tasks: #Ecto.Association.NotLoaded<association :tasks is not loaded>,
    updated_at: ~N[2022-06-10 09:39:23]
  },
  %Appeo.Leads.Project{
    __meta__: #Ecto.Schema.Metadata<:loaded, "projects">,
    activities: #Ecto.Association.NotLoaded<association :activities is not loaded>,
    appointments: #Ecto.Association.NotLoaded<association :appointments is not loaded>,
    buildings: #Ecto.Association.NotLoaded<association :buildings is not loaded>,
    comments: #Ecto.Association.NotLoaded<association :comments is not loaded>,
    company: #Ecto.Association.NotLoaded<association :company is not loaded>,
    company_contact: #Ecto.Association.NotLoaded<association :company_contact is not loaded>,
    company_contact_id: 12,
    company_id: 2,
    contacts: #Ecto.Association.NotLoaded<association :contacts is not loaded>,
    description: "Quia veniam ut erro",
    estimated_time: 37,
    id: 16,
    index: 1.0,
    inserted_at: ~N[2022-06-13 06:35:00],
    lead_column: #Ecto.Association.NotLoaded<association :lead_column is not loaded>,
    lead_column_id: 5,
    name: "Shannon Wynn",
    project_buildings: #Ecto.Association.NotLoaded<association :project_buildings is not loaded>,
    project_contacts: #Ecto.Association.NotLoaded<association :project_contacts is not loaded>,
    project_image: nil,
    project_materials: #Ecto.Association.NotLoaded<association :project_materials is not loaded>,
    project_permission_joined_users: #Ecto.Association.NotLoaded<association :project_permission_joined_users is not loaded>,
    project_stage: nil,
    project_start_date: ~U[2004-02-08 06:13:00Z],
    project_type: "roofing",
    status: :active,
    tasks: #Ecto.Association.NotLoaded<association :tasks is not loaded>,
    updated_at: ~N[2022-06-13 06:35:00]
  }
]

Apologies in advance if I’ve misunderstood the question. You don’t need to store an intermediate list of IDs to find out if a particular project ID is in this list of projects:

id = 14
Enum.any?(projects, fn project -> project.id == id end)
# returns `true`, or `false` if it's not found

Enum.find(projects, fn project -> project.id == id end)
# returns the project struct with id == 14, or `nil` if it's not found

There are alternative ways this can be written with pattern matching or the & capture operator, but this is the most straightforward.

I don’t know your use case, but you may want to query the project directly by ID

def get_project(id), do: Repo.get(Project, id)

Ok. I will try to implement this. Thanks

I’ll add that if the intermediate list of IDs is for some reason required, then this is how you can do it

project_ids = Enum.map(projects, fn project -> project.id end)

Enum.any?(project_ids, fn id -> id == 14 end)

Thank you so much buddy. I was stuck on this problem

My pleasure. If you’re just starting out with Elixir I would recommend a few learning resources to dig into before getting too far with your project. I think you’ll go farther quicker if you take the time on standard Elixir first:

The official getting started guide is very good and IMO should be done from start to finish by anyone new to Elixir

The documentation on the Enum module will teach you to read Elixir documentation, and this is maybe the most important module in the standard library for programming with Elixir’s functional paradigm

Exercism.io will help you practice solving different problems in Elixir