before :, select new{"Required field column ..."} The benefits of reducing unnecessary data queries, especially when distributed, the network is in bad condition, and the unnecessary data is many, select new{"Required field column ..."} Obvious benefits
Title, LINQ multi-link paging, select new{"Required field column:"}, with Yang Tao Mvcpager, foreground traversal show custom fields
(1) My example is that the (Tb_mnager) Administrator table is linked to the (Tb_role) role table and returns a combination of two table part fields
The following controller code:
Public ActionResult List (int pager) {pager = pager?? 1; int pageSize = 20; Mymvccmsentities db = new mymvccmsentities (); Multi-link query, Select new{} returns the required field//Note here, RS is actually a List<object> object, how to traverse the view??? Answer: Reflection var rs = (from M in Db. Tb_manager join R in Db. Tb_role on M.roleid equals r.id m.sequenum Descending select New { M.id,//custom State, SQL statements are converted to case. When: Then status = M.isdisable = = 1? "<font color= ' red ' > Disable </font>": "<font color= ' green ' > Normal </font>", M.loginna Me, M.sequenum, m.date,//role name in the role table R.rolename}); Total queries----SQL Profiler: Database Viewbag.allcount = RS is queried here. Count (); This statement will be self-Create select Top ... and return the result list<object>----The database is queried once viewbag.list = Rs. Topagedlist ((int) pager, pageSize); return View (); }
(2) Well, the result is returned, so how does the foreground traverse the value of the list<object>?
Answer: Reflection details Simple application of C # Reflection
Here, I wrote a helper class with the principle of reflection:
Using system;using system.collections.generic;using system.linq;using system.reflection;using System.Text;using System.threading.tasks;namespace mvc.util{public class Reflection {//<summary>///////For the value of the specified property (not distinguished from large lowercase)///</summary>//<param name= "propertyname" > Attribute name </param>//<param name= "O" & gt; target object </param>///<returns></returns> public static Object Getpropertyvaluebyname (String P Ropertyname, Object o) {if (o = = null) {o = new {}; }//Create a return object Returnobject = new Object (); Propertyinfo[] P1 = O.gettype (). GetProperties (); foreach (PropertyInfo pi in p1) {if (pi. Name.tolower () = = Propertyname.tolower ()) {returnobject = pi. GetValue (o); }} return returnobject; } }}
(3) This is good, so it's much easier for us to traverse and read the values in the views view:
@{int index = 0;} @foreach (Object T in viewbag.list) {index++; <tr> <td align= "center" ><span class= "Checkall" style= "Vertical-align:middle;" ><input class= "List-box" value= "@Reflection. Getpropertyvaluebyname (" id ", T). ToString () "Type=" checkbox "/></span></td> <td> @index </td> <td> @Reflection. Getpropertyvaluebyname ("LoginName", T). ToString () </td> <td> @Reflection. Getpropertyvaluebyname ("RoleName", T). ToString () </td> <td> @Reflection. Getpropertyvaluebyname ("date", T). ToString () </td> <td> @Html. Raw (Reflection.getpropertyvaluebyname ("St ATUs ", T). ToString ()) </td> <td> <input type= "text" VAlue= "@Reflection. Getpropertyvaluebyname (" Sequenum ", T). ToString () "class=" sort "/> </td> <td align=" center "><a href= ' @Url . Action ("edit", "Manager", new {area= "CMS", Id=reflection.getpropertyvaluebyname ("id", T). ToString ()}) ' > Modify </a></td> </tr>}
View pagination code, note html.pager extension method The first parameter is of type ipagedlist:
@Html. Pager ((ipagedlist) viewbag.list, new pageroptions {pageindexparametername = "Pager", Showpageindexbox = False, Pageindexboxtype = Pageindexboxtype.textbox, Showgobutton = false, Firstpagetext = "Home", Lastpagetext = "Last", ShowFirstLas T = true, currentpageritemwrapperformatstring = "<span class=\" current\ ">{0}</span>", CssClass = "Default"} , new {@style = "width:100%;float=left;"})
LINQ multi-Link paging, select new{"Required field columns:"}, with Yang Tao Mvcpager, foreground traversal show custom fields