c# - MVC Merging two foreign tables to one view very slow -


i have managed load data 1 table , return view, working well.

unfortunately through learning process , failing understand other posts relating returning 2 models 1 view , using mvc 5 tutorial rick anderson

my controller taking forever create "merged" data table model , return view, when manipulate view goes through whole process again ever big kick in shin.

i have van model not hold sim or imei number of gprs unit stored in assett table both not index linked (if correct wording). wish return view full list of vans along imei , sim number assett table.

van model

[metadatatype(typeof(vanviewmodel))] public partial class vans__ {     public string imei { get; set; }     public string simno { get; set; } }  public class vanviewmodel {     [required]     public int assetid { get; set; }      [required]     [display(name = "chassis number")]     public string van_serial_number { get; set; }      [required]     [display(name = "seller dealer id")]     public int dealer_id { get; set; } } 

asset model

public partial class asset {     public int assetid { get; set; }     public string assetname { get; set; }     public string sim { get; set; }     public string imei { get; set; } } 

controller

public actionresult index() {     list<asset> assets = new list<asset>();     list<vans__> vans = new list<vans__>();     vans = db.vans__.tolist();     foreach (var myvan in vans)     {         int assetidtosearch = myvan.assetid;         asset myasset = dbasset.assets.where(m => m.assetid == assetidtosearch).single();         myvan.imei = myasset.imei;         myvan.simno = myasset.sim;     }     // query asset table imei , sim number     return view(db.vans__.tolist());  } 

my controller reason slow response understand, appreciate guidance in relation code me understand way return both models , sort them in view. if take time highlight way speed code or how 2 models through view correctly appreciated.

from stephen's suggestion realised controller return vans view. have implemented now, still generating model on request there way of making quicker return view.

here updated controller

        public actionresult index()    {         // below code slow itterates through tables , populates them - can not release tbh.         list<asset> assets = new list<asset>();         list<vans__> vans = new list<vans__>();          vans = db.vans__.tolist();          foreach (var myvan in vans)         {             int assetidtosearch = myvan.assetid;             asset myasset = dbasset.assets.where(m => m.assetid == assetidtosearch).single();             myvan.imei = myasset.imei;             myvan.simno = myasset.sim;         }          return view(vans); // not need return db.vans__.tolist() vans contains already.     } 

i have amended title realised data tables on separate data bases. if please point me in correct direction speed code or example of how join 2 tables can access data in 1 view appreciated.

the main reason slow loading in case loop connects database. see need id vans, in controller, in stead of:

vans = db.vans__.tolist(); 

try using linq query or lamda equivalent in following line:

vans = db.vans__.select(p => p.assetid).tolist(); 

so now, next one, in stead of writing loop can pass query database replacing loop:

        foreach (var myvan in vans)     {         int assetidtosearch = myvan.assetid;         asset myasset = dbasset.assets.where(m => m.assetid == assetidtosearch).single();         myvan.imei = myasset.imei;         myvan.simno = myasset.sim;     } 

with this:

var assets = dbasset.assets.where(m => vans.contains(m.assetid)).tolist(); 

or

var assets = dbasset.assets.where(m => vans.select(p => p.assetid).contains(m.assetid)).tolist();  

- 1 of 2 lines should work, try both of them in code , see one's right:)

and give list of assets. suggest use projection again , fields need using .select() shown before.

also, take @ video: http://youtube.com/watch?v=gmyvmsf4dte&feature=youtu.be explains more optimising queries , why should this.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -