asp.net mvc - How to populate a ViewModel object after joining two entities together -
i have following viewmodel class:-
public class rackjoin { public tmsrack rack { get; set; } public resource resource { get; set; } public technology technology { get; set; } public componentdefinition componentdefinition { get; set; } public sdorganization site { get; set; } public sdorganization customer { get; set; } } and following uncompleted repository model method join 2 entities:-
public iqueryable<tmsrack> allfindracks(string q) { list<rackjoin> rakjoin = new list<rackjoin>(); var result = rack in tms.tmsracks .include(rack => rack.datacenter) .include(rack => rack.zone) .include(rack => rack.tmsfirewalls) .include(rack => rack.tmsrouters) .include(rack => rack.tmsservers) .include(rack => rack.tmsstoragedevices) .include(rack => rack.tmsswitches) .include(rack => rack.technology) join resource in entities.resources .include(a => a.componentdefinition) .include(a => a.resourcelocation.sitedefinition.sdorganization) .include(a => a.resourcelocation.sitedefinition.accountdefinition.sdorganization) on rack.technology.it360id equals resource.resourceid (q == null || rack.technology.tag.toupper().startswith(q.toupper())) select //not sure should write here; first question; not sure how can populate list<rackjoin> based on join outcomes, in above method.
second question ; calling above method following action method:-
public actionresult index(string searchterm = null, int page = 1) { var racks = repository.allfindracks(searchterm).orderby(a => a.technology.tag).topagedlist(page, 5); if (request.isajaxrequest()) { return partialview("_racktable", racks); } return view(racks); } so can know how paging , join work , mean ef join records satisfy clause, skip , take. , entities skip , take applied?
1st question:
you'll have access both range variables in select, have view model properties both rack , resource.
select new rackjoin { rack = rack, resource = resource }; 2nd question:
as long return iqueryable, ef should see skip , take operators , apply them sql query, sql server responsible joins , paging.
note: can remove new list line of code.
i have more examples of using join in standard linq operators article. hope helps.
Comments
Post a Comment