sql - Left OuterJoin in Entity/Linq -


so confused. have 2 tables, 1 locations assignments consists of: location id, type , type id. have table called services, consists of name, id , description , icon.

the idea say, me 13 services, create 13 checkboxes. say, check location assignments table, if services (based on type, id , location id) matches service in list, check checkbox, else leave unchecked.

what ahve far is:

    public static ienumerable<constants.assignable> getallservice(int id)     {         list<constants.assignable> assign = new list<constants.assignable>();          using (var db = new context())         {             var serv =  s in db.services                         join la in db.locationassignments on s.id equals la.typeid locationassignments                         la in locationassignments                         la.locationid == id && s.id == la.typeid && la.type == constants.serv                         select s;              foreach(var s in serv)             {                  assign.add(new constants.assignable(){                     id = s.id, name = s.name                 });             }              return assign;         }     } 

which returns me, currently, 2 services, when should return me 13. there wrong join.

from there do:

    <h3 class="muted">services nearby</h3>     ienumerable<ufa.location.core.constants.assignable> servicesnearby = ufalocationapp.helpers.locationhelper.queryhelper.getallservicesnearby(model.id);     foreach (var servicenb in servicesnearby)     {         <div class="control-group">             <label class="control-label" for="servicenearby">             @servicenb.name             </label>             <div class="controls">                 <input type="checkbox" id="locationservice" value="@servicenb.id" name="servicenb" checked="@(servicenb.assigned ? "checked" : "")" />             </div>         </div>     }  

which prints out 2 check boxes are, in case checked. there should 11 more unchecked.

what have change in query say: me services , check off ones associated location?

to make left join, need use defaultifempty(), seems component you're missing make work;

var serv =  s in db.services             join la in db.locationassignments on s.id equals la.typeid                  locationassignments             la in locationassignments.defaultifempty()             la.locationid == id && s.id == la.typeid                 && la.type == constants.serv             select s; 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -