LINQ multi-Link paging, select new{"Required field columns:"}, with Yang Tao Mvcpager, foreground traversal show custom fields

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.