Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8272701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:13:07+00:00 2026-06-08T07:13:07+00:00

I have two entity that has an one-to-many relationship, I´m trying to select the

  • 0

I have two entity that has an one-to-many relationship, I´m trying to select the master entity filtering properties in the child items.

For example:

public class Pedido
{
    public int Id { get; set; }
    public string Descricao { get; set; }
    public virtual ICollection<PedidoItem> Itens { get; set; }
}


public class PedidoItem
{
    public int Id { get; set; }
    public int PedidoId { get; set; }
    public Pedido Pedido { get; set; }
    public string Descricao { get; set; }
    public int Status { get; set; }
}


public class DataInit : DropCreateDatabaseAlways<Data>
{
    protected override void Seed(Data context)
    {
        context.Pedidos.Add(new Pedido {
            Descricao = "PEDIDO UM",
            Itens = new List<PedidoItem> { 
                new PedidoItem { 
                    Descricao = "ITEM UM", 
                    Status = 0 },
                new PedidoItem{
                    Descricao = "ITEM DOIS",
                    Status = 0 },
                new PedidoItem{
                    Descricao = "ITEM TRES",
                    Status = 0 },
                new PedidoItem{
                    Descricao = "ITEM QUATRO",
                    Status = 1 }
            }
        });

        context.SaveChanges();

        base.Seed(context);
    }

    public DataInit()
    {

    }
}
public class Data : DbContext
{
    public DbSet<Pedido> Pedidos { get; set; }
    public Data()
    {
        Database.SetInitializer(new DataInit());
    }
}

class Program
{
    static void Main(string[] args)
    {
        Data dt = new Data();
        var pedidos = from ped in dt.Pedidos
                      where ped.Itens.Any(item => item.Status == 1)
                      select ped;

        var lista = pedidos.ToList();
    }
}

I have only one Pedido entity in database and one one item with status = 1, I´d like to return only this item that has status = 1 in the collection, How should I do?

I´d like to return Pedido entity with only one item that was filtered(item.Status == 1)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:13:08+00:00Added an answer on June 8, 2026 at 7:13 am

    It is a bit hard to get what you want exactly, but I think it is this: the Pedidos that only have one Iten (Item?) and of which that one item has Status == 1.

    That would be:

    from ped in dt.Pedidos
    where ped.Itens.Count() == 1
    from it in ped.Itens.Where(i => i.Status == 1)
    select ped // or new { ped.Property1, ..., it.Description }
    

    In fluent syntax this translates to a SelectMany, but this is one of those cases where the comprehensive (or query) syntax is much easier to follow. (Although the Were is fluent syntax).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Entity Framework objects with a one-to-many relationship: widget(parent) and widgetNail(child) Widget
i have two entities named Parent and Child , linked in a one-to-many relationship.
I have a Core Data entity, OrderItem that has a to-many relationship to another
I've got two entities with a one-to-many relationship between them. The entity that holds
I have a Department entity which has a one-to-many relationship to an Employee entity.
I have core data model with two entities that have a many-to-many relationship with
I have a data model which has two entities in a relationship of one
I have two tables that are considered a single entity in my domain model.
I have two tables in my database schema that represent an entity having a
I have two entities types: RunContainer parent entity type Run child entity type Run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.