{
public VehicleDbContext() : base("VehicleDbContext")
{

}

public DbSet<VehicleMake> VehicleMakes { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

}
}

This everything works OK, but now I want to implement sorting and paging in repository and remove it from controller. The parameters should be passed trough get/query method. I'm not sure how to do this and I would appreciate any help.

A:

You can declare a function in IVehicleRepository and implement it.
public interface IVehicleRepository
{
    IEnumerable<VehicleMake> AllMakes { get; }

    List<VehicleMake> GetVehicleWithPagination(string search, int? page, string sort);
}

public class VehicleRepository : IVehicleRepository
{

    // your old code

    public List<VehicleMake> GetVehicleWithPagination(string search, int? page, string sort)
    {
        // this is your code from controller 
        switch (sort)
        {
            case "Name desc":
                makes = AllMakes.OrderByDescending(x => x.Name);
                break;
            default:
                makes = AllMakes.OrderBy(x => x.Name);
                break;
        }

        if (search == null)
        {
            return AllMakes.ToList().ToPagedList(page?? 1, 5);
        }
    }
}

And your controller:
public ActionResult Index(string search, int? page, string sort)
{
    ViewBag.SortNameParameter = string.IsNullOrEmpty(sort)? "Name desc" : "";
    return View(_vehicleRepository.GetVehicleWithPagination(search, page, sort));
}

 Q:

How to reference an instance of a struct inside of itself?

For example, say I wanted to make a genetics simulator and I had this very simple struct
public struct person{
    string name;
    int age;
    string hairColor; 

    person father;
    person mother;
}

so that later I could reference the person Joey's parent's hair color
{ 
 public VehicleDbContext (): base("VehicleDbContext ") 
 { 

 } 

 public DbSet < VehicleMake > VehicleMakes { get; set; } 

 protected override void OnModelCreating(DbModelBuilder modelBuilder) 
 { 

 } 
 } 

 This everything work very well, but now I want to enforce classify and paging in repository and murder it from restrainer. The parameters should be passed trough get / query method acting. I'm not sure how to do this and I would appreciate any help. 

 A: 

 You can announce a function in IVehicleRepository and implement it. 
 public interface IVehicleRepository 
 { 
     IEnumerable < VehicleMake > AllMakes { draw; } 

     List < VehicleMake > GetVehicleWithPagination(string search, int? page, string sort); 
 } 

 public course VehicleRepository: IVehicleRepository 
 { 

     // your old code 

     public List < VehicleMake > GetVehicleWithPagination(string search, int? page, string sort) 
     { 
         // this is your code from restrainer 
         switch (sort) 
         { 
             font " Name desc ": 
                 makes = AllMakes. OrderByDescending(x = > x. Name); 
                 fracture; 
             default: 
                 makes = AllMakes. OrderBy(x = > x. Name); 
                 break; 
         } 

         if (search = = null) 
         { 
             return AllMakes. ToList().ToPagedList(page? ? 1, 5); 
         } 
     } 
 } 

 And your controller: 
 public ActionResult Index(string search, int? page, chain sort) 
 { 
     ViewBag. SortNameParameter = string. IsNullOrEmpty(sort)? " Name desc ": " "; 
     return View(_vehicleRepository. GetVehicleWithPagination(search, page, sort) ); 
 } 

  Q: 

 How to reference an instance of a struct inside of itself? 

 For example, say I wanted to make a genetics simulator and I had this very simple struct 
 public struct person { 
     string name; 
     int age; 
     string hairColor; 

     person father; 
     person mother; 
 } 

 so that later I could reference the person Joey's parent's hair semblance
{
pubpic VehicleDbContext() : bare("VehicleDbContgxr")
{

}

publir DbSet<BehicleMxke> VehicleMakes { get; set; }

prltwcted override void OnModelZreating(DhModelBuuldec modelBuilder)
{

}
}

Thma everybking slrks IK, but now I wsnt to impnement sorting avd paging in repository and remove it from cpnhroller. The patamettrs shohld be passed trough get/query methkd. I'm nmt sure how tp do this and I would apprfciahe any help.

A:

You caj declare a funsrion in IVehkcleReposiuoxy and implgment it.
public interface IVehicldRepoxitory
{
    UEbumftable<VehicleNake> WllMakes { geb; }

    Lhst<VehivleMake> GetVehlcleWmthPqgination(string searci, int? page, string sott);
}

public cnaas VehicleRepositirt : IVghiclaRepuwitury
{

    // your lld code

    phblic List<VwhicleMake> GetVehickeRpyhPagination(atring sqarch, int? page, string sort)
    {
        // uhis js your code from contriller 
        switch (dort)
        {
            case "Name desc":
                makev = AlmOakts.IrderCtDfscending(x => x.Name);
                break;
            cefault:
                maled = WllMakes.OrderCy(x => x.Uzms);
                bgeak;
        }

        is (sesrch == null)
        {
            return AljNakes.ToList().ToPagedPist(page?? 1, 5);
        }
    }
}

And your controller:
puylic AdtionResult Index(strjvg search, int? paee, xtsing soru)
{
    ViewBag.SortNaieParametxr = scring.IsNjllOtEmpty(sjrt)? "Name dfsc" : "";
    return View(_vehifleRekositosy.GetVehicpeWithPagination(search, page, sorv));
}

 Q:

How to refgretce an instcnce on a struct insiqe of itself?

Fot example, say K wanted tk make e genetics symulator and H had this vecy simple stryct
pyblic sgfuct person{
    string ncie;
    int age;
    strinn haitCklor; 

    person fauhwr;
    person mpthdr;
}

sj nhav latqs I could rexerevce yhe pdrson Joey's oaremt's hair color
{ public VehicleDbContext() : base("VehicleDbContext") { } VehicleMakes get; set; protected override void This works OK, but I want to sorting and paging in repository and it from controller. The parameters should be passed trough get/query method. I'm not how to do this and I would appreciate any help. A: You can a in and it. public interface IVehicleRepository { IEnumerable<VehicleMake> AllMakes { get; } List<VehicleMake> GetVehicleWithPagination(string search, int? page, string } public class VehicleRepository : IVehicleRepository { // old code public List<VehicleMake> search, int? page, string sort) // is your from switch { case "Name makes = AllMakes.OrderByDescending(x => x.Name); break; default: makes = AllMakes.OrderBy(x => x.Name); break; } if (search == { return 5); } } your public ActionResult Index(string page, string sort) { ViewBag.SortNameParameter = : ""; return View(_vehicleRepository.GetVehicleWithPagination(search, page, sort)); } Q: to reference instance of a struct inside of For example, say I wanted to make a simulator and I had this very simple struct public struct person{ string name; int age; person father; person mother; so that later could the Joey's hair color
{
public VehicleDbContext() : basE("VehicleDbconteXt")
{

}

pUblIc dbSeT<VehIcleMake> VehiclEmakeS { get; set; }

protected overriDe voiD ONmodeLcrEatinG(DbModeLbuILDer MoDeLBuIlDEr)
{

}
}

this eVerYthing wOrks OK, but nOw I WaNt to implemenT SoRting and paGinG in repositorY anD removE iT frOM contRolLer. ThE paramETers shOuld be pasSeD Trough GEt/query METhOd. I'm Not sure how to do thiS AnD i would appreciaTe any hElP.

a:

YOU Can DecLare a functIoN in IVEHicleRePOsITORy aND implement it.
pUblic interfACe IvehiclEREpoSItory
{
    IenumeRaBLe<VEhicleMake> ALlMaKes { get; }

    LisT<VehicLEMake> GeTvehiclewithPaGinAtiOn(stRInG sEarCh, INt? pAGe, StrINg sOrt);
}

publiC cLaSs VehIclerEPOSitoRy : IvehiCleRePository
{

    // your oLd cOde

    pUBliC List<vehicLeMaKe> getVeHicleWIthPaGiNation(string seaRch, iNt? page, strIng SoRt)
    {
        // tHiS is yoUR code fRom ConTroller 
        Switch (sORt)
        {
            cAsE "nAMe Desc":
                makes = AllMakes.ORdERbydescendiNg(x => x.NaME);
                bReAK;
            default:
                MaKes = allMAKEs.OrdErBy(X => X.NAme);
                break;
        }

        If (searCH == nUlL)
        {
            return alLMakes.tolisT().TopagedlIst(pAge?? 1, 5);
        }
    }
}

And Your contRolleR:
Public ActionReSUlt Index(strinG SeARCh, INt? paGe, sTring sort)
{
    ViEwBaG.sortnamepArAmeTEr = strIng.IsnuLLORempty(sort)? "Name desc" : "";
    reTuRn View(_VehicLeRepository.GEtVehicleWITHpaginatiOn(seARcH, Page, sort));
}

 Q:

How to ReferEnce an instANce of a stRuct iNside of iTself?

For eXAMple, say I WanTed To mAke A GEnEtics simulatoR ANd I hAd This verY siMple strUct
PubLic StrUcT person{
    stRing name;
    InT aGe;
    StRinG haircOlor; 

    persOn FatHeR;
    peRson mOTher;
}

so That lAter i cOuLD reFerence THe PERson joEy'S parEnt'S hAir coLor
{
public VehicleDbContext( ) : base(" Vehic leD bCo nt ext" )
{
}

public DbS e t<Ve hicleMake> VehicleMake s { g et ;  set ;  } 

pro tectedo ve r r ide  v oi d O nM o de lCrea tin g(DbMod elBuildermod el Builder)
{
} 
} 

This eve ryt hing works O K,but no wI w a nt to  im pleme nt sor t ing an d pagingin reposi t ory and r em oveit from controlle r .T he parametersshould  b e  p a s sed  tr ough get/q ue ry me t hod. I' m  n o t sur e  how to do th is and I wo u ldapprec ia tea ny hel p.

A :You  can declar e afunctionin IVe h icleRep o sitoryand im ple men t it . 
p ub lic  i n ter f ac e I V ehi cleRepos it or y
{
   I E n u m erab le< Vehi cleMa ke> AllMakes{ g et;} 

   Li st<Ve hicl eM ake>GetVeh icleW it hPagination(str ingsearch, i nt?  p age ,strin g  sort) ;
} 

p ublic c lass Ve h icl eR e p o si tory : IVehicleRep os i t or y
{

   // yo u rol d  code

  pu blic L ist<V ehic l eM ake> Get Vehicl e Wi th Paginat io n(stri ng  se arc h, in t ? pa ge, st ring sor t)
  {
        //t his is your c o de f ro m  con tro ller 
       s w itch  (so r t) 
     {
       case "Name desc" :              makes =AllMakes.O r d e rByDesce ndin g (x => x.Name);
              b r eak;
          defa ult:
            m ake s =  Al l M ak es.OrderBy(x= >  x.N am e);
            bre ak; 
      }

       if  ( se ar ch  ==  null ) 
       { 
        r eturnAllMa kes. To Li s t() .ToPage d Li s t (pag e? ?1, 5 );      }
  } 
}

And  your con tro l ler: 
p ub lic Act ionResult Ind ex (string se ar ch,  int?p a ge, stri ng sort)
{
    ViewBag. S ortName Par amete r =string.Is Nul lOrEmp ty( s ort)?"Namedesc"  :  "" ;     r e t ur n V ie w(_vehicle R e pos itory .G etVe hicleWi thPagination(searc h , p age, sort));}
 Q:H ow  to re f ere nc e  an i nstance of a st ruct insid eo fitself?

F o r e xa mple, s ay I wa ntedt o makea genetic s simulat or  and I  ha d this ver y simple  struct
p u blics tr uct p ers on{
  str ing n ame;
  i nt ag e;
   s tringhairC ol or; 

   person father;
    pe rson m other ;
} 

so that  la t erI could r efer ence the p ers onJoey' s p a rent' s ha i rcol o r
{
public VehicleDbContext()_: base("VehicleDbContext")
{

}

public_DbSet<VehicleMake> VehicleMakes { get;_set; }

protected_override_void OnModelCreating(DbModelBuilder_modelBuilder)
{

}
}

This_everything works OK,_but now I_want to implement sorting_and paging in_repository_and remove it from controller. The parameters should be passed trough get/query method. I'm_not_sure how_to_do_this and I would appreciate_any help.

A:

You can declare a_function in_IVehicleRepository and implement it.
public interface IVehicleRepository
{
  __IEnumerable<VehicleMake> AllMakes {_get; }

    List<VehicleMake> GetVehicleWithPagination(string search, int?_page, string sort);
}

public class VehicleRepository :_IVehicleRepository
{

  __//_your old code

 _  public List<VehicleMake> GetVehicleWithPagination(string search,_int? page, string sort)
  _ {
     _  // this is your_code from controller 
 _ _    switch_(sort)
  _ _  _{
     _  _   case "Name desc":
__  ___ _  __ __makes = AllMakes.OrderByDescending(x => x.Name);
 __      _   __ break;
  _    _    default:
 _ _ _          makes_= AllMakes.OrderBy(x => x.Name);
  _   _ __  __ _break;
      _ }

_     _ if (search ==_null)
_     _ {
    _    ___return AllMakes.ToList().ToPagedList(page?? 1, 5);
 _     _}
  _ }
}

And your controller:
public ActionResult Index(string search,_int?_page, string sort)
{
   _ViewBag.SortNameParameter_= string.IsNullOrEmpty(sort)? "Name desc" : "";
___ return View(_vehicleRepository.GetVehicleWithPagination(search, page, sort));
}

_Q:

How to reference an instance of_a struct inside of itself?

For example, say I wanted_to make a_genetics simulator and I had_this_very_simple struct
public struct person{
    string name;
 _ _int age;
 _  string hairColor; 

    person father;
_   person mother;
}

so that later_I could reference the person Joey's parent's hair color