Search code examples
silverlightlinqriadomain-service-class

How to formulate join queries in domain service class?


in the domain service class there are two entities which I would like to use in join query: WorkerOnMachine {id(int), WorkerId(int), MachineId(int)} and Worker(WorkerId(int), Name(string)...)

I wanted to create a method in domain service class which returned all workers that operate on selected machine (the worker-machine relationships are stored in WorkerOnMachineTable) the following way:

public IQueryable<Worker> GetWorkerByMachineId(int machineId)
        {
            var joinedTable = from wom in this.ObjectContext.WorkerOnMachine
                              join w in this.ObjectContext.Worker on wom.WorkerId equals w.Id
                              where wom.MachineId == machineId
                              select new { w };
            return joinedTable as IQueryable<Worker>;
        }

but the method returns an empty list. Does anyone know what is the right way to write the above method in domain service class?

Thank you!


Solution

  • Don't use var, it is confusing you.

    As written, joinedTables is an instance of IQueryable<AnonType>. Since it is not an IQueryable<Worker> the cast by as returns null.

    Instead, write:

    IQueryable<Worker> joinedTable = ...
    

    Then fix:

    select w;
    

    and finally:

    return joinedTable;
    

    (btw, that join looks good).