Search

just show me the code

Wednesday, December 10, 2008

Linq to Lambda

I changed a Linq expression to a Lambda expression so I could refactor the filter. Here is the before and after

Before: (Linq expression)
results = from u in ctx.ActiveUsers
          where (u.CompanyID != 1 &&
                   (u.LastName.ToLower().Contains(searchString)
                   || u.Email.ToLower().Contains(searchString)
                   || u.Company.Name.ToLower().Contains(searchString)))
          orderby u.LastName, u.FirstName
          select new Employee
          {
              ID = u.ID,
              FirstName = u.FirstName,
              LastName = u.LastName,
              Email = u.Email,
              CompanyName = u.Company.Name,
              CompanyID = u.CompanyID.ToString()
          };


After: (Lambda expression)
results = ctx.ActiveUsers
    .Where(Employee.GetExpression(searchString))
    .OrderBy(u =>  u.LastName ).ThenBy(u => u.FirstName)
    .Select(u => new Employee {
ID = u.ID
      , FirstName = u.FirstName
, LastName = u.LastName
      , Email = u.Email
, CompanyName = u.Company.Name
      , CompanyID = u.CompanyID.ToString() });


plus this to keep the where expression the same on the count:
private static Expression<Func<User, bool>> GetExpression(string searchString)
{ 
    Expression<Func<User, bool>> p = (u => u.CompanyID != 1 &&
                       (u.LastName.ToLower().Contains(searchString)
                       || u.Email.ToLower().Contains(searchString)
                       || u.Company.Name.ToLower().Contains(searchString)));
    return p;
}

so that GetExpression can be used here to make sure that our count query is the same as the select
public static int GetCustomerCount()
{
    UserContext ctx = new UserContext();
    int totalRecords;
 
    string searchString = SearchString; 
 
    totalRecords = ctx.ActiveUsers.Count(Employee.GetExpression(searchString));
    return totalRecords;
}




Tuesday, December 9, 2008

use your Gmail as a free smtp server

this is what your web.config would look like
<smtp  from="username@gmail.com">
  <network host="smtp.gmail.com"  port="465" userName="username@gmail.com"
        password="password" defaultCredentials="false"/>
</smtp>
thanks to this post for the help.

Monday, December 8, 2008

Linqpad


Linqpad is a nice way to learn how to write linq statements. There is no install required. Check it out here. Thanks Joseph

Thursday, December 4, 2008

pdf files in the sql database


<asp:TemplateField HeaderText="ID" SortExpression="ID"> 
    <ItemTemplate>
        <asp:HyperLink  ID="hl1" runat="server" 
            NavigateUrl='<%# String.Format("readfile.ashx?id={0}", Eval("ID") ) %>' 
            Text='<%# Eval("ID") %>' >
        </asp:HyperLink>
    </ItemTemplate>
</asp:TemplateField>



href="readfile.ashx?id=24"

this is what my handler code looks like

    /// 
    /// Summary description for $codebehindclassname$
    /// 
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class readfile : IHttpHandler
    { 
        public void ProcessRequest(HttpContext context)
        { 
            ReportContext ctxReport = new ReportContext();
            var r = from re in ctxReport.Report
                    where re.ID == Convert.ToInt32(context.Request["id"].ToString()) 
                    select re;
            Report rReport = r.FirstOrDefault<Report>();
 
            context.Response.Clear();
            string t = rReport.ReportType.TypeName;
            context.Response.ContentType = "application/pdf"; 
 
            context.Response.OutputStream.Write( 
                rReport.ReportData.ToArray() , 0
                , Convert.ToInt32(rReport.FileLength));
            context.Response.End(); 
        } 
        public bool IsReusable
        {
            get
            {
                return true;
            }
        } 
    }

Wednesday, December 3, 2008

Rocco


born on Dec 1st 10:22 am CST

Tuesday, December 2, 2008

jQuery Hello Goodbye

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
    $(".notice").fadeOut("slow");
    $("button").click(function () {
        $(".notice").fadeIn("slow");
        $(".notice").fadeOut("slow");
    });    
});
</script>
<style>
html { height: 100%; margin-bottom: 1px; }
.basic, .notice { padding: .8em; margin-bottom: 1em; border: 2px solid #dddddd; }
.notice { background: #FFF6BF; color: #817134; border-color: #FFD324; }
</style>
<div id="main">
    <button>Click Me!</button>
    <div class="basic" style="position:relative; width:550px;height:150px;" >
      <div class="notice" style="position:absolute; top:10px;left:10px;width:450px;">
          <p>Hello</p>
          <p>Goodbye</p>
      </div>
    </div>
    end
</div>
click here to see it in action.

Contributors