Monday, May 26, 2014

LINQ GET MIDLE RECORD (PAGING)

 var pagesize = Convert.ToInt32(ConfigurationManager.AppSettings["PageSize"]);
                
result = result.Skip(pagesize * currentPage).Take(pagesize).ToList();

LINQ ORDER BY EXPRESSION PARAM

var param = Expression.Parameter(typeof(PERSON));
                
Func<PERSON, Object> sortExpression = Expression.Lambda<Func<AuditLogs, Object>>(Expression.Convert(Expression.Property(param, sortColumn), typeof(Object)), param).Compile();

if (sort == "DESC")
  result = result.OrderByDescending(sortExpression).ToList();
       else
  result = result.OrderBy(sortExpression).ToList();


OTHERS METHOD


public Func<T, Object> GetExpression<T>(String sortby)
{
  var param = Expression.Parameter(typeof(T));
  var sortExpression = Expression.Lambda<Func<T, Object>>(Expression.Convert(Expression.Property(param, sortby), typeof(Object)), param);
  
  return sortExpression.Compile();

}

  result = result.OrderBy(GetExpression<PERSON>(sortColumn)).ToList();

Friday, May 23, 2014

LINQ Distinct with a property

Comparer(No work)


Group By Method(work)
result = result.GroupBy(cust => cust.Name).Select(grp => grp.First()).ToList();

Monday, May 19, 2014

C# - Render HTML tags in GridView


HTML Code <b> will not show in the Grid, so need to HTML Decode.

BoundField

<asp:BoundField DataField="MyColumn" HtmlEncode="True" />

ItemTemplate

<ItemTemplate>         
     <asp:Label runat="server"  Text='<%#System.Web.HttpUtility.HtmlEncode(Convert.ToString(Eval("Name"))) %>' /> 
</ItemTemplate> 

Thursday, May 8, 2014

Debug WCF Service in IIS


  1. Set the breakpoints in the WCF service.
  2. Change the build path of the WCF service application to the bin directory of the service hosted in the IIS.(  (Properties Host > Build OutputPath > IIS Bin Folder)
  3.  Build the WCF application in the debug mode.
  4.  Run the project application.
  5.  Attach the 'W3WP' process to it. If in case you are not getting the process select "All Process" checkbox.

ASP.NET Session State Vs ViewState

Session State
  1. Across Multiple Page.
  2. Saved in a specific session, cleared when the session dies (usually after 20 minutes of inactivity).
  3. Saved in Server.
  4. valid for any type of objects.
public List<Contact> SessionContacts
{
    get { return (List<Contact>)HttpContext.Current.Session["SessionConacts"]; }
    set { HttpContext.Current.Session["SessionConacts"] = value; }

}

View State

  1. Only that web page.
  2. It is stored in a hidden field.
  3. Saved in page, travels up and down between client and server.
  4. No expiration date.
  5. Valid for Serializable data only.

public List<Contact> SessionContacts
{
   get { return (List<Contact>)ViewState["VSConacts"]; }
   set { ViewState["VSConacts"] = value; }

}

Conclusion
If data you store in the view state is not large enough to cause slow performance , use view state.
If you store too much data in session and you create many sessions in a short period with many of your concurrent users, you would be killing your server.