0

Could someone please point me in the right direction of querying a many to many relationship. I'm trying to get a List of all active Machine Operators and for the life of me cannot seem to get it right.

If its of any use, this is what I've been trying up to now

public HttpResponseMessage Get()
    {
        using (DAL.Repositories.Repository<Machine> machineRepo = new DAL.Repositories.Repository<Machine>())
        using (DAL.Repositories.Repository<Operator> operatorRepo = new DAL.Repositories.Repository<Operator>())
        {
            List<int> OperatorIDs = new List<int>();
            List<Machine> Machine = new List<Machine>();

            OperatorIDs = operatorRepo.FindAll(x => x.Active).Select(y => y.ID).ToList();

            var machineOperators = machineRepo.FindAll(x => x.Active).Select(x => x.Operators.All(o => OperatorIDs.Contains(o.ID)));


            return Request.CreateResponse(HttpStatusCode.OK, machineOperators);
        }
    }

My code as follows

public class Operator
{
    public Operator()
    {
        Machines = new HashSet<Machine>();
    }

    public int ID { get; set; }
    public string FirstName { get; set; }
    public string Surname { get; set; }
    public bool Active { get; set; }

    // Navigational Link
    public virtual ICollection<Machine> Machines { get; set; }
}

public class Machine
{
    public Machine()
    {
        Departments = new HashSet<Department>();
        Operators = new HashSet<Operator>();
    }

    public int ID { get; set; }
    public string MachineName { get; set; }
    public bool Active { get; set; }

    public ICollection<Operator> Operators { get; set; }
}

And in my efContext

public MachineConfiguration()
    {
        // Key
        HasKey(x => x.ID);

        // Fields
        Property(x => x.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        Property(x => x.MachineName).IsRequired();

        // Table
        ToTable("Machine");

        // Relationships
        HasMany(e => e.Operators)
            .WithMany(e => e.Machines)
            .Map(m => m.ToTable("MachineOperators")
                .MapLeftKey("MachineID")
                .MapRightKey("OperatorID"));
    }
2
  • What do you want to return? All the active machines that have all the operators in active?
    – ocuenca
    Apr 9, 2015 at 15:02
  • A list of Active Machines with their corresponding Active Operators Apr 9, 2015 at 15:05

1 Answer 1

0

Try this:

public HttpResponseMessage Get()
{
    using (DAL.Repositories.Repository<Machine> machineRepo = new DAL.Repositories.Repository<Machine>())
    {
        var machineOperators = machineRepo.FindAll(x => x.Active).Include(x=>x.Operators).Where(x => x.Operators.All(o =>o.Active)).ToList();

        return Request.CreateResponse(HttpStatusCode.OK, machineOperators);
    }
}
8
  • Returns the active machines, but no operators { "$id": "1", "ID": 1, "MachineName": "Roland X1000", "Active": true, "Operators": [], "Departments": [] } Apr 9, 2015 at 15:09
  • Maybe you have disabled lazy loading, try eager loading that nav property using the Include extension method.
    – ocuenca
    Apr 9, 2015 at 15:12
  • Now I saw your Operator nav property is not virtual, If you want that EF tracks the changes in your entities and lazy load your navigation properties, you need to declare them as virtual: public virtual ICollection<Operator> Operators { get; set; }
    – ocuenca
    Apr 9, 2015 at 15:21
  • Yes, that worked great. Now i'm faced with this error when the results are returned. The 'ObjectContent1' type failed to serialize the response body for content type 'application/json; charset=utf-8 Apr 9, 2015 at 15:39
  • Check the answers in these posts: post1, post2 and post3
    – ocuenca
    Apr 9, 2015 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.